VBA Next without For compile error

PB7

Board Regular
Joined
Mar 1, 2011
Messages
58
VBA colleagues:

I've got this VBA compile error: Next without For, when my code seems ok, though I realize some nuance could be missing or wrong.

Here's the code getting the error message:

For i = 5 To nlines
Range("A" & i).Select
If TestDates(ActiveCell.Value, "04/30/2011") = "1" Then
ActiveCell.Offset(0, 13).Value = ActiveCell.Offset(0, 10).Value
ActiveCell.Offset(0, 19).Value = ActiveCell.Offset(0, 10).Value
Else
If TestDates(ActiveCell.Value, "04/30/2011") = "0" Then
ActiveCell.Offset(0, 12).Value = ActiveCell.Offset(0, 10).Value
ActiveCell.Offset(0, 19).Value = ActiveCell.Offset(0, 10).Value
End If
Next i

Above, TestDates is a function doing date differential calculations on 2 different dates, returning the difference stated as a month (0,1,2,3 etc)
The TestDates function works fine in another module that calls it.
Thank you in advance for any guidance, feedback.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Extra "If".

Gary



Code:
For i = 5 To nlines
        Range("A" & i).Select
            If TestDates(ActiveCell.Value, "04/30/2011") = "1" Then
                ActiveCell.Offset(0, 13).Value = ActiveCell.Offset(0, 10).Value
                ActiveCell.Offset(0, 19).Value = ActiveCell.Offset(0, 10).Value
            Else
            [B][COLOR=Red]If[/COLOR][/B] TestDates(ActiveCell.Value, "04/30/2011") = "0" Then
                ActiveCell.Offset(0, 12).Value = ActiveCell.Offset(0, 10).Value
                ActiveCell.Offset(0, 19).Value = ActiveCell.Offset(0, 10).Value
            End If
    Next i
 
Upvote 0
This error is so missleading...

You're actually missing an End If..

Indentation of code helps to see this type of error.

Rich (BB code):
For i = 5 To nlines
    Range("A" & i).Select
    If TestDates(ActiveCell.Value, "04/30/2011") = "1" Then
        ActiveCell.Offset(0, 13).Value = ActiveCell.Offset(0, 10).Value
        ActiveCell.Offset(0, 19).Value = ActiveCell.Offset(0, 10).Value
    Else
        If TestDates(ActiveCell.Value, "04/30/2011") = "0" Then
            ActiveCell.Offset(0, 12).Value = ActiveCell.Offset(0, 10).Value
            ActiveCell.Offset(0, 19).Value = ActiveCell.Offset(0, 10).Value
        End If '<--This was missing
    End If
Next i

Hope that helps.
 
Upvote 0
Maybe

Code:
For i = 5 To nlines
    Range("A" & i).Select
    If TestDates(ActiveCell.Value, "04/30/2011") = "1" Then
        ActiveCell.Offset(0, 13).Value = ActiveCell.Offset(0, 10).Value
        ActiveCell.Offset(0, 19).Value = ActiveCell.Offset(0, 10).Value
    ElseIf TestDates(ActiveCell.Text = "04/30/2011") = "0" Then
        ActiveCell.Offset(0, 12).Value = ActiveCell.Offset(0, 10).Value
        ActiveCell.Offset(0, 19).Value = ActiveCell.Offset(0, 10).Value
    End If
Next i

Note: you do not have to select a cell in order to test its properties.
 
Upvote 0
Try changing
Code:
For i = 5 To nlines
Range("A" & i).Select
If TestDates(ActiveCell.Value, "04/30/2011") = "1" Then
ActiveCell.Offset(0, 13).Value = ActiveCell.Offset(0, 10).Value
ActiveCell.Offset(0, 19).Value = ActiveCell.Offset(0, 10).Value
Else
If TestDates(ActiveCell.Value, "04/30/2011") = "0" Then
ActiveCell.Offset(0, 12).Value = ActiveCell.Offset(0, 10).Value
ActiveCell.Offset(0, 19).Value = ActiveCell.Offset(0, 10).Value
End If
Next i
[CODE]
to
For i = 5 To nlines
Range("A" & i).Select
If TestDates(ActiveCell.Value, "04/30/2011") = "1" Then
ActiveCell.Offset(0, 13).Value = ActiveCell.Offset(0, 10).Value
ActiveCell.Offset(0, 19).Value = ActiveCell.Offset(0, 10).Value
Else
ActiveCell.Offset(0, 12).Value = ActiveCell.Offset(0, 10).Value
ActiveCell.Offset(0, 19).Value = ActiveCell.Offset(0, 10).Value
End If
Next i
[/CODE]
 
Upvote 0
You've got 2 If's but only 1 End If. Also, yo don't need to select cells. Try the following

Code:
For i = 5 To nlines

    If TestDates(Cells(i, 1).Value, "04/30/2011") = "1" Then
        Cells(14, i).Value = Cells(i, 11).Value
        Cells(i, 20).Value = Cells(i, 11).Value
    End If

    If TestDates(Cells(i, 1).Value, "04/30/2011") = "0" Then
        Cells(i, 13).Value = Cells(i, 11).Value
        Cells(i, 20).Value = Cells(i, 11).Value
    End If
Next i
 
Upvote 0
Just an observation...

Is 1 or 0 the only 2 possible results of the TestDates function?
If so, there is no need for the 2nd IF..
Because if the First IF TestDates(...) = "1" is FALSE, then it MUST be "0", no need to run TestDates again.

Again, IF 1 or 0 are the only possible results...
 
Upvote 0
Jonmo / all,

Right, code was missing the second End If, and after adding, worked fine. Sure, the error message is definitely a curve ball to the unitiated.

The problem code was indented when I pasted in, but in future will have to check how to keep it indented in the thread.

Anyways, thanks everyone for the great help here! Alms for the VBA-poor.
 
Upvote 0
Use code tags...

[#code]
Your Code Here
See indents
[#/code]

Remove the # signs

makes it look like

Code:
     Your Code Here
          See indents
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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