Next without for error

dannums

New Member
Joined
Feb 24, 2012
Messages
4
Hey guys I have been pulling my hair out trying to figure out why I am getting a next without for compile error. I'm running Excel 2010 and believe I have looped my Do While statement, looped my For Next statement, and also closed my if statement. My simplified code block is below. What am I missing here? Also, if someone could help me with the tag to preserve tabs, i would be most gracious...the code tag didn't work for me<CODE>

<CODE>
<CODE>
Do While strFile <> ""
Workbooks.Open (strMyDir & strFile)
Workbooks(strFile).Activate
intWkSheetCount = 1
For intWkSheetCount = 1 To Worksheets.Count
Worksheets(intWkSheetCount).Activate
Application.ScreenUpdating = True
If ActiveSheet.Name = "Instructions" Or ActiveSheet.Name = "CountryPortfolioAssumptions" Or
ActiveSheet.Name = "CountryLevelSummary" Or ActiveSheet.Name = "SiteReference" Then
Next intWkSheetCount
Else
'copy paste process here
End If
Next intWkSheetCount
Workbooks(strFile).Close SaveChanges:=False
strFile = Dir() 'cycle to next file
Loop

</CODE)< CODE>
</CODE></CODE>
</CODE>
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi dannums and welcome to the Board
Firstly, by indenting your code you will find it much easier to debug.
I'm assuming your IF statement wants to skip the sheets if they have those names, so try
Code:
Sub CopytoNewSht()
Do While strFile <> ""
Workbooks.Open (strMyDir & strFile)
Workbooks(strFile).Activate
intWkSheetCount = 1
    For intWkSheetCount = 1 To Worksheets.Count
        Worksheets(intWkSheetCount).Activate
        Application.ScreenUpdating = True
            If ActiveSheet.Name <> "Instructions" Or ActiveSheet.Name <> "CountryPortfolioAssumptions" Or _
                ActiveSheet.Name <> "CountryLevelSummary" Or ActiveSheet.Name <> "SiteReference" Then
                'copy paste process here
            End If
    Next intWkSheetCount
Workbooks(strFile).Close SaveChanges:=False
strFile = Dir() 'cycle to next file
Loop
End Sub
 
Upvote 0
Thanks Michael. That re-structuring of the code seems to have done the trick! The code was indented when I pasted it in my post between the "code" tags...but the formatting seems to have been lost when the post went live? Not sure why that was.
 
Upvote 0
Ok, glad it worked....thanks for the feedback...(y)
 
Upvote 0

Forum statistics

Threads
1,213,483
Messages
6,113,919
Members
448,533
Latest member
thietbibeboiwasaco

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