Help with VBA loop

JRidge

Board Regular
Joined
Sep 10, 2013
Messages
106
Hi All,

I have this code...

Code:
Dim rgFoundCell As Range    
Const strFindMe As String = "Select Seprations"
     
Application.ScreenUpdating = False
With Sheet5
        Set rgFoundCell = .Range("F:F").Find(what:=strFindMe)
        Do Until rgFoundCell Is Nothing
            
        Set rRange = rgFoundCell.Offset(1)
        If rRange.Value = "No" Then..............................Doing things in here

        Set rgFoundCell = .Range("F:F").FindNext        
        Loop
        End With
    
End Sub

The code is working in that it finds the first instance of strFindMe, Does everything i want it to do..

When it loops it then finds the same first instance and not the next..

Any ideas why??

Any Help would be appreciated.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Code:
Sub JR()
    Const strFindMe As String = "Select Seprations"
    Dim rLook       As Range
    Dim rFind       As Range
    Dim sAddr       As String

    Set rLook = Sheet5.Range("F:F")
    Set rFind = rLook.Find(what:=strFindMe, After:=rLook(rLook.Cells.Count))

    If Not rFind Is Nothing Then
        sAddr = rFind.Address
        
        Do
            With rFind.Offset(1)
                If .Value2 <> "No" Then
                    If VarType(.Value2) = vbDouble Then
                        ' it's a number; do your thing
                        ' ...
                        ' ...
                        ' ...
                    End If
                End If
            End With

            Set rFind = rLook.FindNext(After:=rFind)
        Loop While rFind.Address <> sAddr
    End If
End Sub
 
Upvote 0
There's no need to check for <>"No" is it's a number:

Code:
Sub JR()
    Const strFindMe As String = "Select Seprations"
    Dim rLook       As Range
    Dim rFind       As Range
    Dim sAddr       As String

    Set rLook = Sheet5.Range("F:F")
    Set rFind = rLook.Find(what:=strFindMe, After:=rLook.Cells.Count)

    If Not rFind Is Nothing Then
        sAddr = rFind.Address

        Do
            With rFind.Offset(1)
                If VarType(.Value2) = vbDouble Then
                    ' it's a number; do your thing
                    ' ...
                    ' ...
                    ' ...
                End If
            End With

            Set rFind = rLook.FindNext(After:=rFind)
        Loop While rFind.Address <> sAddr
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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