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:

Some videos you may like

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,974
Office Version
2013
Platform
Windows
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
 

dannums

New Member
Joined
Feb 24, 2012
Messages
4
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.
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,974
Office Version
2013
Platform
Windows
Ok, glad it worked....thanks for the feedback...(y)
 

Watch MrExcel Video

Forum statistics

Threads
1,102,694
Messages
5,488,314
Members
407,634
Latest member
ps01

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top