Just trying to loop this. Already works but manual

hitbid

Board Regular
Joined
Jan 21, 2016
Messages
114
Hi, Just working on a random process to improve, and I am simply not used to looping enough to be able to integrate it. Hoping someone might be able to help.

The code below is currently working, if I run it manually step by step.
I start in A1.
I then find the first "br :", and copy it, and then move down cells. Sub FindBR()
Then the AutoFill subroutine will call the Finding_Bottom_and_Top, and paste what I need where I need.

Once done, I have to go back to the FindBR routine, then go back to the AutoFill. I keep repeating this until it finally errors out around row 5000.


Code:
Dim BottomRow As Long
Dim TopRow As Long




Sub Finding_Bottom_And_Top()
    Selection.End(xlDown).Select
    BottomRow = Selection.Row
    Selection.End(xlUp).Select
    TopRow = Selection.Row
End Sub


Sub Start()
Range("A1").Select
End Sub


Sub FindBR()
Cells.Find(What:="br :", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
        Selection.Copy
        Selection.End(xlDown).Select
End Sub


Sub AutoFill()
    Call Finding_Bottom_And_Top
    ActiveCell.Offset(0, -1).Range("A1").Select
    ActiveSheet.Paste
    Selection.AutoFill Destination:=Range("A" & TopRow & " :A" & BottomRow), Type:=xlFillCopy
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I kept searching.

I consolidated FindBR and AutoFill into this:
Code:
Sub FindBR()

Cells.Find(What:="Br :", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
        Selection.Copy
        Selection.End(xlDown).Select
        
        Call Finding_Bottom_And_Top
        ActiveCell.Offset(0, -1).Range("A1").Select
        ActiveSheet.Paste
        Selection.AutoFill Destination:=Range("A" & TopRow & " :A" & BottomRow), Type:=xlFillCopy
        Selection.End(xlDown).Select
End Sub

Then I created this little looping script here. I went and counted the number of instances of data that I was looking for, which will tell me how many times to repeat the loop.
Code:
Sub LastCellInColumn()Dim LastCell As Long
Dim iVal As Integer
    Range("B900000").End(xlUp).Select
    LastCell = Selection.Row
    iVal = Application.WorksheetFunction.CountIf(Range("B1:B" & LastCell), "*Br :*")
    'MsgBox iVal
    
    For i = 1 To iVal
    FindBR
    Next i
        
End Sub

This....seems to work!!!
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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