Next without For Error

laxcat73

Board Regular
Joined
Aug 5, 2011
Messages
143
Can't figure it out. Maybe a fresh set of eyes can see what I don't?

Code:
[COLOR=blue]Private Sub[/COLOR] cmdCreate_Click()
[COLOR=blue]If[/COLOR] Trim(Me.txtRef.Value) = "" [COLOR=blue]Then[/COLOR]
  Me.txtRef.SetFocus
  MsgBox "Please enter a reference #"
  [COLOR=blue]Exit Sub[/COLOR]
[COLOR=blue]End If[/COLOR]
 
    Application.EnableEvents = [COLOR=blue]False[/COLOR]
    [COLOR=blue]Dim[/COLOR] LR&, i&
    LR = Worksheets("Master").Range("F" & Rows.Count).End(xlUp).Row
    [COLOR=blue]For[/COLOR] i = 4 T[COLOR=blue]o[/COLOR] LR
        [COLOR=blue]If [/COLOR]Worksheets("Master").Range("F" & i).Value = Me.txtRef.text [COLOR=blue]And[/COLOR] _
            Worksheets("Master").Range("P" & i).Value = "" [COLOR=blue]And[/COLOR] _
            Worksheets("Master").Range("Q" & i).Value = "" [COLOR=blue]Then[/COLOR]
            Worksheets("Master").Range("P" & i).Value = Me.txtFirst.Value
            Worksheets("Master").Range("Q" & i).Value = Me.txtPaid.Value
 
        [COLOR=blue]ElseIf [/COLOR]Worksheets("Master").Range("F" & i).Value = Me.txtRef.text [COLOR=blue]And[/COLOR] _
            Worksheets("Master").Range("V" & i).Value = "" [COLOR=blue]And[/COLOR] _
            Worksheets("Master").Range("W" & i).Value = "" [COLOR=blue]Then[/COLOR]
            Worksheets("Master").Range("V" & i).Value = Me.txtFirst.Value
            Worksheets("Master").Range("W" & i).Value = Me.txtPaid.Value
 
        [COLOR=blue]ElseIf[/COLOR] Worksheets("Master").Range("F" & i).Value = Me.txtRef.text [COLOR=blue]And[/COLOR] _
            Worksheets("Master").Range("AB" & i).Value = "" [COLOR=blue]And[/COLOR] _
            Worksheets("Master").Range("AC" & i).Value = "" [COLOR=blue]Then[/COLOR]
            Worksheets("Master").Range("AB" & i).Value = Me.txtFirst.Value
            Worksheets("Master").Range("AC" & i).Value = Me.txtPaid.Value
 
        [COLOR=blue]ElseIf[/COLOR] Worksheets("Master").Range("F" & i).Value = Me.txtRef.text [COLOR=blue]And[/COLOR] _
            Worksheets("Master").Range("AH" & i).Value = "" [COLOR=blue]And[/COLOR] _
            Worksheets("Master").Range("AI" & i).Value = "" [COLOR=blue]Then[/COLOR]
            Worksheets("Master").Range("AH" & i).Value = Me.txtFirst.Value
            Worksheets("Master").Range("AI" & i).Value = Me.txtPaid.Value
 
        [COLOR=blue]Else[/COLOR]: MsgBox "Employee has completed all scheduled payments.  Please check for errors in previous dates."
            Me.txtFirst.SetFocus
            [COLOR=blue]Exit Sub[/COLOR]
        [COLOR=blue]End If[/COLOR]
 
    Application.EnableEvents = [COLOR=blue]True[/COLOR]
Me.txtRef.Value = ""
Me.txtFirst.Value = ""
Me.txtPaid.Value = ""
Me.txtRef.SetFocus
[COLOR=blue]End Sub[/COLOR]
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You're getting an error because you need to close out your "For i = 4 To LR" loop with a "Next i"

Try putting "Next i" in a line before Application.EnableEvents = True
 
Upvote 0
You're getting an error because you need to close out your "For i = 4 To LR" loop with a "Next i"

Try putting "Next i" in a line before Application.EnableEvents = True

I see. When I do that, the Error message is looped in as well and even if the 4th cell (4 structured payments) is empty, when I fill in the last one the MsgBox error loops as well.
 
Upvote 0
Is there any way to have this part of the if statement NOT looped?

Code:
[COLOR=blue]Else[/COLOR]: MsgBox "Employee has completed all scheduled payments.  Please check for errors in previous dates."
            Me.txtFirst.SetFocus
            [COLOR=blue]Exit Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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