Alien_Explorer
Alien_Explorer

Reputation: 867

VBA If Then combined with For Each Loop

Can someone advise on the following, please?

I have this For loop in the following way but I need to add another condition ("If" I suppose)(see comment):

**'if cell in "A2:A1000" = "cat" and is not empty then run this:**

    For Each cel In .Range("D2:D1000") 
    
    Time = cel.Value
    If Not IsEmpty(Time) Then
    
    'code does some stuff
    
        End If
    Next

**'Else if cell in "A2:A1000" <> "cat" but not blank run this:**

'some code

Upvotes: 0

Views: 125

Answers (1)

Pierre Bonaparte
Pierre Bonaparte

Reputation: 633

Try:

Sub forfor()
        
Dim SrchRng As Range, celq As Range
Set SrchRng = Range("A1:A1000")
        
For Each celq In SrchRng
If InStr(1, celq.Value, "cat") > 0 Then
    
    For Each cel In .Range("D2:D1000") 
    Time = cel.Value
    If Not IsEmpty(Time) Then
    'code does some stuff
    End If
    Next

End If
Next celq
    
End Sub

Upvotes: 1

Related Questions