next without For - help please?

jayd4wg

Board Regular
Joined
Mar 25, 2005
Messages
197
Code:
For Each cell In [Data!a5:a279999]
    If cell.EntireRow.Hidden = False Then
        pm_name = cell.Offset(0, 7)

'define variables values in subroutine def_Vars
    def_Vars
'catch end of document to exit loop
        If pm_name = "" Then
            Exit For
        End If
'write the placemark info to file #1
'if account is not parent then write folder closing statements to kml and set trigger to only write it once
'then write as child folder output
    If pm_par = 0 Then
    write_folderClose
    End If
    write_pmOutput
        
Next cell
'write footer to file
Print #1, "</folder>"
Print #1, "</Document>"
Print #1, "</kml>"

Close #1
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic


the write_folderClose sub is to write a one time bit of code to close out a kml folder (for google earth)

Code:
Sub write_folderClose()
If parent_Trigger = True Then
Print #1, "</folder>"
Print #1, "<folder><name>Child Accounts</name><open>0</open>"
Else
parent_Trigger = False
End If

End Sub

the write_pmOutput sub has nothing more in it than a bunch of print statements to write the KML code for a bunch of variables collected from the spreadsheet.

it befuddles me as to how i'm losing the for/next relationship...any thoughts? This macro is failing to compile because of a next without for error. i'm STUMPED :(
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
You're actually missing an End If

Rich (BB code):
For Each cell In [Data!a5:a279999]
    If cell.EntireRow.Hidden = False Then
        pm_name = cell.Offset(0, 7)
    End If
 
Upvote 0
If you indent your code (Office Automation Ltd. - Smart Indenter), it looks like this:

Code:
    For Each cell In [Data!a5:a279999]
        If cell.EntireRow.Hidden = False Then
            pm_name = cell.Offset(0, 7)

            'define variables values in subroutine def_Vars
            def_Vars
            'catch end of document to exit loop
            If pm_name = "" Then
                Exit For
            End If
            'write the placemark info to file #1
            'if account is not parent then write folder closing statements to kml and set trigger to only write it once
            'then write as child folder output
            If pm_par = 0 Then
                write_folderClose
            End If
            write_pmOutput

        Next cell
        'write footer to file
        Print #1, ""
        Print #1, ""
        Print #1, ""

        Close #1
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic

See the problem?
 
Upvote 0
The problem actually is that you are missing an "End If" statement for the first "If..Then" statement, the one immediately under the "For" statement. The problem is the VB does not examine each structure individually, rather it treats them all like they were the same and simply counts the opening and closing statements for all blocks... when they do not balance, VB simply assumes the last block structure is at fault, hence the "For Missing Next" error.
 
Upvote 0
could it have anything to do with not declaring "cell" as a variable? should it be declared? this worked just fine until i chopped the macro up into a few subs.

the whole reason i went to the sub structure for all the code is that it was getting really messy to look at and debug, with alot of duplicated code. I went from a flat kml document with parent accounts and child accounts mapping all on the same folder and sr. leadership has asked that i put parents in a separate folder from child accts. so that the child accounts layer (folder) can be turned off in google earth so they don't display.
 
Upvote 0
By looking at the code my guess as well would be the missing End If. As a beginner I was continually stumped by the strange “next without For” Compiling error in Code where clearly the For and Next were indeed there! It took a while before I noticed the peculiarity that VBA gave this error message if there was a missing End If within the Loop. I have been finding a lot of these small quirks that are typically not shown or discussed in most books. Hence the usefulness of this Forum where experienced users give the benefit of their knowledge.
Making a habit of always typing things like For and Next or If and End If at the same time with a staggering indent I found to mostly ensure that I do not make so many mistakes.
 
Upvote 0
Making a habit of always typing things like For and Next or If and End If at the same time with a staggering indent I found to mostly ensure that I do not make so many mistakes.
That's what I do... I type the opening block statement, hit the Enter Key twice, then type the closing block statement, then hit the Up Arrow Key (puts me on the blank line created by the double Enter Key) and press the Space Bar twice (I use a double space for my indents)... at this point, I am ready to begin typing the code that goes into the block structure. After many years of programming, this has become second nature... I haven't seen a "You Are Missing" error in too many years to count now.
 
Upvote 0
What makes it even harder to troubleshoot (aside from the indentation) is that you can't just count the IF's and assume you need the same number of End If's. Because you can have 1 line IF's that have the 'actioniftrue' part written on the same line as the IF condition. So no End If is required. Then, once you know you're missing an End If, it's not always as easy as it was in this case to figure out where to put a new End If. Putting it in the wrong place can completely change what the code is intended to do, even though it will compile without error.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,324
Messages
6,124,250
Members
449,149
Latest member
mwdbActuary

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