Macro Script to find a character and prefix

Solved/Closed
Pradeep - May 31, 2010 at 01:28 AM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - Jun 1, 2010 at 07:17 AM
Hello,

I have a spread sheet which looks like below

Program: STK4R01 C:\LM90\STK4R01 Block: _Main
05-31-10 13:18 GE FANUC SERIES 90-70 (v7.05) Page 99


|[ START OF LD PROGRAM STK4R01 ] (* *)
|
|[ VARIABLE DECLARATIONS ]

V A R I A B L E D E C L A R A T I O N T A B L E

REFERENCE NICKNAME REFERENCE DESCRIPTION
--------- -------- --------------------------------
%I00001 LTD HSC Strobe1 STATUS
%I00002 LTD HSC Strobe2 STATUS


Program: STK4R01 C:\LM90\STK4R01 Block: DPILE
05-31-10 13:18 GE FANUC SERIES 90-70 (v7.05) Page 99

+[ START OF LD BLOCK DPILE ]
|
|[ VARIABLE DECLARATIONS ]

V A R I A B L E D E C L A R A T I O N T A B L E

REFERENCE NICKNAME REFERENCE DESCRIPTION
--------- -------- --------------------------------
%L00001 D PILE INNER SLEW POS
%L00003 D PILE OUTER SLEW POS


I want to get the block name and prefix all the % ones with the block name.

For example if the Block name is _Main
then all the reference coloumns will become _main.%L00001

then it will look for the next block name and if its diff all the reference block will have the reference name assoicated with the prefix of the block name.


Please help me out on the same.Its urgent.
Related:

5 responses

rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
May 31, 2010 at 01:18 PM
try this

Sub FixMe()
Dim lMaxRows As Long
Dim lRow As Long
Dim sBlock As String
    
    lMaxRows = Cells.Find("*", Cells(1, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For lRow = 1 To lMaxRows
        
        ' if the string on row contains " Block: " then we have our new block
        If (InStr(1, Cells(lRow, "A"), " Block: ") > 0) Then
        
            sBlock = Mid(Cells(lRow, "A"), InStr(1, Cells(lRow, "A"), " Block: ") + Len(" Block: "))
            sBlock = sBlock & "."
            
            
        'if the first character of the cell on the row is % then, append the last block name
        ElseIf (Left(Cells(lRow, "A"), 1) = "%") Then
            
            Cells(lRow, "A") = sBlock & Cells(lRow, "A")
            
        End If
    
    Next lRow

End Sub

2
Hi riz,

Thanks for your response
I ran the macro script and it doesnt seem to do anything..

The excel sheet looks like as shown below all in the same row which is A
Program: STK4R01 C:\LM90\STK4R01 Block: _MAIN
05-31-10 14:45 GE FANUC SERIES 90-70 (v7.05) Page 2



%I00549 LTD LOS's # 1 - 6 OK
%I00550 LTD FWD CONTCTR CLOSED
%I00551 LTD REV CONTCTR CLOSED
%I00552 LTD FINAL EAST LS OK
%I00553 LTD FINAL WEST LS OK
Program: STK4R01 C:\LM90\STK4R01 Block: DPILE
05-31-10 14:45 GE FANUC SERIES 90-70 (v7.05) Page 54



%M07468 TRL CNV RO 3or4 FAULTED
%M07469 TRL CNV RO 5or6 FAULTED
%M07470 TRL CNV RO 1or2 BYPASS
%M07471 TRL CNV RO 3or4 BYPASS
%M07472 TRL CNV RO 5or6 BYPASS
%M07473 TRL CNV RS 1or2 FAULTED
%M07474 TRIPPER CHUTE PLUG FAULTED

So if the "Block:" name is _MAIN then it will add a prefix of _MAIN.%I00549 in the same row
if the "Block: " name is DPILE then it will add a prefix of DPILE.%M07468 in the same rows and it moves on .

Currently when i ran the script it doesnt seem to make a change.Please reply me on the same
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
May 31, 2010 at 09:21 PM
I ran it on the data that you gave it works
Could you please upload a sample file with sample data etc on some shared site like https://authentification.site , http://wikisend.com/ , http://www.editgrid.com etc and post back here the link to allow better understanding of how it is now and how you foresee.
0
Hi Riz,

I have uploaded the sample file in below link .

https://authentification.site/files/22732556/Stk4r01.csv

Can you please try it now.
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
May 31, 2010 at 10:23 PM
It is because of leading white spaces.
0
Hi Riz,
Thanks for your response
Can you please what exactly has to be done so that the script will work.
0

Didn't find the answer you are looking for?

Ask a question
Hi Riz,

Thanks for your help.I did figure it out now and the scipt works perfectly fine.
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Jun 1, 2010 at 07:17 AM
This would work irrespective of leading white space

Sub FixMe()
Dim lMaxRows As Long
Dim lRow As Long
Dim sBlock As String
Dim BlockPos As Variant
Dim PctPos As Variant

    lMaxRows = Cells.Find("*", Cells(1, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For lRow = 1 To lMaxRows
        
        BlockPos = InStr(1, Cells(lRow, "A"), " Block: ")
        PctPos = InStr(1, Cells(lRow, "A"), "%")
        
        ' if the string on row contains " Block: " then we have our new block
        If (BlockPos > 0) Then
        
            sBlock = Mid(Cells(lRow, "A"), BlockPos + Len(" Block: "))
            sBlock = Trim(sBlock) & "."
            
            
        'if the first character of the cell on the row is % then, append the last block name
        ElseIf (Left(Trim(Cells(lRow, "A")), 1) = "%") Then
            
            If (PctPos > 1) Then
            
                Cells(lRow, "A") = Left(Cells(lRow, "A"), PctPos - 1) & sBlock & Mid(Cells(lRow, "A"), PctPos)
            
            Else
            
                Cells(lRow, "A") = sBlock & Mid(Cells(lRow, "A"), PctPos)
                
            End If
            
            
        End If
    
    Next lRow

End Sub
0