1
Dim valCheck As Variant

For i = 0 To row1
    For j = 0 To col1
        MsgBox ("started" & i)
        valCheck = Range("A1").Offset(i, j).Value
        MsgBox (valCheck)
        If valCheck = "Details" Then
            MsgBox ("found")
            ActiveCell = Range("A1").Offset(i + 1, j)
            Exit For
            Exit For
        End If
    Next j
Next i
2
  • You can't use 2 x Exit For like this, it will only use the first one, and exit the For j loop. what are you trying to achieve ? Commented Mar 31, 2018 at 6:02
  • I want to loop through a row1 X col1 matrix until i find desired word="Details" and select the column in which it is present Commented Mar 31, 2018 at 6:05

2 Answers 2

3

You don't need to have 2 For loops and Exit For, you can simply use the Find function instead.

Dim FindRng As Range

Set FindRng = Cells.Find("Details")
If Not FindRng Is Nothing Then ' Find was successful
    FindRng.Select ' <-- not sure what you want to do after you find the cell with "Details" ?
Else ' Find failed
    MsgBox "Unable to find 'details'", vbCritical
End If
Sign up to request clarification or add additional context in comments.

Comments

2

As soon as the first Exit For executes, it bypasses the second. You need to place the second Exit For outside the inner loop.

Dim valCheck As Variant

For i = 0 To row1
    For j = 0 To col1
        MsgBox ("started" & i)
        valCheck = Range("A1").Offset(i, j).Value
        MsgBox (valCheck)
        If valCheck = "Details" Then
            MsgBox ("found")
            ActiveCell = Range("A1").Offset(i + 1, j)
            Exit For
        End If
    Next j
    If valCheck = "Details" Then Exit For
Next i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.