0

The aim of my code is to print available if there is any data betweeen L1 to W1000 of length 7.

Although it finds the value of length 7, my code doesn't obey exit for.

what is the reason?

Private Sub CommandButton1_Click()

Dim Prod As Variant
Dim Dev As Variant
Dim counter As Integer
Dim j As Variant

Prod = Array("PBA_100", "PCA_500", "PRD_500", "PGA_500", "PVD_500")

For j = LBound(Prod) To UBound(Prod)
     MsgBox Prod(j)

 With ThisWorkbook.Sheets(Prod(j))
 LastRow = ThisWorkbook.Sheets(Prod(j)).Columns("A").Cells.Find("*", 
 SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
    For Each cell In .Range("N2:N" & LastRow)
        arr = Split(Replace(cell.Value, "  ", " "), " ")
        For Each arrElem In arr
            If Len(arrElem) = 7 Then
            MsgBox arrElem
        Exit For
      Else
      MsgBox arrElem
            End If
        Next arrElem
    Next cell
 End With

Next j

End Sub
2
  • Where k is defined? Commented Sep 6, 2017 at 10:29
  • k is defined at the top i have only included the part where i have the issue. Commented Sep 6, 2017 at 10:29

1 Answer 1

1

You need to exit the nested for loop, if you find available value. Like this:

With ThisWorkbook.Sheets(Prod(j))
LastRow = ThisWorkbook.Sheets(Prod(j)).Columns("A").Cells.Find("*", 
SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
    For Each cell In .Range("L1:W1000", .Cells(.Rows.Count, "A").End(xlUp))
        arr = Split(Replace(cell.Value, "  ", " "), " ")
        For Each arrElem In arr
            If Len(arrElem) = 7 Then
            ThisWorkbook.Sheets("Sheet1").Range("D" & k).Value = "available"
            Exit For 'You exit only the nested loop here!
            Else
            ThisWorkbook.Sheets("Sheet1").Range("D" & k).Value = "NOT available"
            End If
        Next arrElem
    Next cell
End With
Sign up to request clarification or add additional context in comments.

1 Comment

@AnirudhChauhan - start debugging step by step with F8 and try paying attention what arrElem is and its value. For example add debug.print arrElem before the If Len(arrElem)=7. Hopefully, after about 30 minutes of pressing F8 you would see the solution.

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.