• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 622
  • Last Modified:

Excel VBA - Change my For statement to ?

I need to change this "For" statement to something that when found true, then go on to do the next
For statement.


For b = 15 To 24
If Activesheet.Range("B" & b) <> "" Then
    Activesheet.Range("D" & b).Select
         Activesheet.Range("D" & b) = CommitNumber.TextBox1.Value '
End If
Next b


this is the code following:
'///////////////////////////////////////////////////////////////////////
For b = 15 To 24
If Activesheet.Range("B" & b) <> "" Then
    Activesheet.Range("D" & b).Select
         Activesheet.Range("D" & b) = CommitNumber.TextBox1.Value '
End If
Next b

For a = 16 To 24
Dim LastRow As Range
Set LastRow = Range("D24").End(xlUp)

If (Activesheet.Range("B" & a)) <> "" Then
    If (Activesheet.Range("D" & a)) = "" Then

    Activesheet.Range("D" & a) = LastRow + 1
    End If
Else
 
End If

Next a
0
ca1358
Asked:
ca1358
  • 2
  • 2
1 Solution
 
Wayne Taylor (webtubbs)Commented:
Hi ca1358,

There doesn't appear to be anything wrong with your For/Next loops. Using If/End If is the normal method for skipping an iteration, based on a condition.

Regards,

Wayne
0
 
ca1358Author Commented:
I need for this statement when it finds the first one to be true stop looping and go on to the next "For" Statement.

For b = 15 To 24
If Activesheet.Range("B" & b) <> "" Then
    Activesheet.Range("D" & b).Select
         Activesheet.Range("D" & b) = CommitNumber.TextBox1.Value '
End If
Next b
0
 
Wayne Taylor (webtubbs)Commented:
Oh, sorry, I misunderstood your question.

Use Exit For....

    For b = 15 To 24
        If Activesheet.Range("B" & b) <> "" Then
            Activesheet.Range("D" & b).Select
            Activesheet.Range("D" & b) = CommitNumber.TextBox1.Value
            Exit For
        End If
    Next b

Wayne
0
 
ca1358Author Commented:
Thank you!!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now