VBA If specific string is matched display top cell and left most cell

excelboi

New Member
Joined
Dec 1, 2016
Messages
3
Hello,

I have been trying to solve a problem through VBA but i am not getting desired output.
For example if any search function found the string "(1) " then the selected cells ID, variable and cell should be displayed. The following is the sample table

IDtstrtdur
1
2(1) 845 (2) 79
3(1) 4 (2) 6
4(1) 987 (2) 556(1) 25 (2) 35

<tbody>
</tbody>

Desired output in new column

IDtstrttendOUTPUT
1
2(1) 845 (2) 792 tstrt (1) 845 (2) 79
3(1) 4 (2) 63 tend (1) 4 (2) 6
4(1) 987 (2) 556(1) 25 (2) 354 tstrt (1)987 (2) 556
4 tend (1)25 (2) 35

<tbody>
</tbody>

Thank you for your time. I will be very thankful if any one can help me on this.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
This should work. Tested against your example data and it ends up looking like your desired output.

Code:
Sub excelboi()
Dim ws As Worksheet
Dim lngROW As Long, lngCOL As Long
Dim rng As Range, cell As Range
Dim first As Variant

Set ws = Sheets("excelboi")

With ws
    lngROW = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    lngCOL = ws.cells(1, ws.Columns.Count).End(xlToLeft).Offset(, 1).Column
    ws.cells(1, lngCOL).Value = "Output"
    
    Set rng = ws.Range(ws.cells(1, 1), ws.cells(lngROW, lngCOL - 1))
    Set cell = rng.Find("*(1) *", ws.cells(1, 1))
     If Not cell Is Nothing Then
        first = cell.Address
        Do
            If ws.cells(cell.Row, 4).Value = "" Then
            ws.cells(cell.Row, 4).Value = ws.cells(cell.Row, 1).Value & " " _
                & ws.cells(1, cell.Column).Value & " " & cell.Value
            Else
                ws.cells(cell.Row, 4).Value = ws.cells(cell.Row, 4).Value & _
                    Chr(10) & ws.cells(cell.Row, 1).Value & " " & _
                    ws.cells(1, cell.Column).Value & " " & cell.Value
            End If
            Set cell = rng.FindNext(cell)
        Loop While Not cell Is Nothing And Not cell.Address = first
     End If
     ws.Columns.ColumnWidth = 100
     ws.Columns.AutoFit
     ws.Rows.AutoFit
End With
 
Upvote 0
I have run the above code but its not working

Runtime error 9:
subscript out of range

This should work. Tested against your example data and it ends up looking like your desired output.

Code:
Sub excelboi()
Dim ws As Worksheet
Dim lngROW As Long, lngCOL As Long
Dim rng As Range, cell As Range
Dim first As Variant

Set ws = Sheets("excelboi")

With ws
    lngROW = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    lngCOL = ws.cells(1, ws.Columns.Count).End(xlToLeft).Offset(, 1).Column
    ws.cells(1, lngCOL).Value = "Output"
    
    Set rng = ws.Range(ws.cells(1, 1), ws.cells(lngROW, lngCOL - 1))
    Set cell = rng.Find("*(1) *", ws.cells(1, 1))
     If Not cell Is Nothing Then
        first = cell.Address
        Do
            If ws.cells(cell.Row, 4).Value = "" Then
            ws.cells(cell.Row, 4).Value = ws.cells(cell.Row, 1).Value & " " _
                & ws.cells(1, cell.Column).Value & " " & cell.Value
            Else
                ws.cells(cell.Row, 4).Value = ws.cells(cell.Row, 4).Value & _
                    Chr(10) & ws.cells(cell.Row, 1).Value & " " & _
                    ws.cells(1, cell.Column).Value & " " & cell.Value
            End If
            Set cell = rng.FindNext(cell)
        Loop While Not cell Is Nothing And Not cell.Address = first
     End If
     ws.Columns.ColumnWidth = 100
     ws.Columns.AutoFit
     ws.Rows.AutoFit
End With
 
Upvote 0
Got it, had to change the sheet name into "excelboi" now its working
Thank you for your time and effort.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,499
Members
449,089
Latest member
Raviguru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top