VBA loop until last instance of Find function

Ada01

Board Regular
Joined
Sep 15, 2008
Messages
60
I have the following find function followed by a cell offset and formula to grab the correct cell reference (this stems from Text to Cols on an old school text file from and old school system)

Code:
Sub Find_Term()'
' find term and concatenate necessary cells


    Columns("A:A").Select
    
' to be looped until the last instance of STPL


    Selection.Find(What:="Location STPL", After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Offset(1, 0).Select
    ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[5]&RC[6])"


End Sub

I would like to repeat the find and concatenate functions until the last time the "location stpl" is found and then stop.

Thanks
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
John

I have tried that option, and had odd results. As near as I can tell, the results of the below version of the code simply finds and acknowledges the existence of the find, changes all of them to TRUE and then starts concatenating each individual row starting on row one.

Code:
Sub TEMP_Search_STPL()

Dim my_findRng As Range
Dim my_find As Range


Set my_findRng = Range("A:A")




With my_findRng
    Set my_find = .Find(What:="Location STPL")
    If Not my_find Is Nothing Then
        firstAddress = my_find.Address


        Do
            ActiveCell.Offset(1, 0).Select
            my_find.Value = ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[5]&RC[6])"
            Set my_find = .FindNext(my_find)
        Loop While Not my_find Is Nothing And my_find.Address <> firstAddress
    End If
    
End With
    


End Sub

A little guidance on how to correct would be much appreciated.

Thanks
 
Upvote 0
Hello Ada01
This is an alternative way of doing it
Code:
Sub Find_Term() '
' find term and concatenate necessary cells

    Dim Cntr As Long
    Dim i As Long

' to be looped until the last instance of STPL

    Cntr = WorksheetFunction.CountIf(Columns(1), "Location STPL")

    For i = 1 To Cntr
        Columns(1).Find(What:="Location STPL", After:=ActiveCell, LookIn:= _
            xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False).Activate
        ActiveCell.Offset(1, 0).Select
        ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[5]&RC[6])"
    Next i

End Sub
HTH
 
Upvote 0
Perhaps:

Code:
Dim rng As Range
Dim LastCell As Range
Dim Foundcell As Range
Dim FirstAddr As String
Set rng = Range("A:A")


With rng
    Set LastCell = .Cells(.Cells.Count)
    Set Foundcell = .Find(What:="Location STPL", After:=LastCell)
    If Not Foundcell Is Nothing Then
        FirstAddr = Foundcell.Address
    End If
    Do Until Foundcell Is Nothing
        Foundcell.Offset(1, 0).FormulaR1C1 = "=CONCATENATE(RC[5]&RC[6])"
        Set Foundcell = .FindNext(After:=Foundcell)
        If Foundcell.Address = FirstAddr Then Exit Do
    Loop
End With
 
Upvote 0

Forum statistics

Threads
1,203,203
Messages
6,054,120
Members
444,703
Latest member
pinkyar23

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