Do While Loop nested in a For Each/Next

Des1gn1ng0ne

Board Regular
Joined
Jul 10, 2008
Messages
73
Code:
Sub Loop_FirstRun()
'Double check all columns before running.
Call ToggleEvents(False)
Dim ws As Worksheet
Set twb = ThisWorkbook
twb.ActiveSheet.Name = Range("A2").Value
twb.ActiveSheet.Range("C:C,D:D,E:E,G:G,I:I,K:K").Delete Shift:=xlToLeft
For Each wb In Workbooks
    Debug.Print wb.Name, twb.Name
    If wb.Name <> twb.Name Then
        wb.Sheets(1).Copy after:=twb.Sheets(Worksheets.Count)
        Application.ActiveWorkbook.ActiveSheet.Name = Range("A2").Value
        Application.ActiveWorkbook.ActiveSheet.Range("C:C,D:D,E:E,G:G,I:I,K:K").Delete Shift:=xlToLeft
        wb.Close Savechanges:=False
    End If
x = 2
    Do While Cells(x, 2).Value <> ""
        If Cells(x, 5).Value <> "*Item*" Then
        Rows(x).Delete Shift:=xlUp
    x = x + 1
    Loop
Next
    'Range("A1").AutoFilter
    'Range("A1").AutoFilter Field:=5, Criteria1:="<>Item", Operator:=xlAnd
Sheet1.Select
Call ToggleEvents(True)
End Sub
The above code returns a "Compile error: Loop without Do." I have tried to isolate the do while loop to a separate macro, and even by itself it does not run. Any help would be greatly appreciated!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
The problem isn't really to do with the loops, your missing an End If after this line.
Code:
Rows(x).Delete Shift:=xlUp
I'm not sure if it should go straight after that line or the next line, but it should be there.:)
 
Upvote 0
You were missing an End If. Try

Code:
Sub Loop_FirstRun()
'Double check all columns before running.
Call ToggleEvents(False)
Dim ws As Worksheet, twb As Workbook, wb As Workbook, x As Integer
Set twb = ThisWorkbook
twb.ActiveSheet.Name = Range("A2").Value
twb.ActiveSheet.Range("C:C,D:D,E:E,G:G,I:I,K:K").Delete Shift:=xlToLeft
For Each wb In Workbooks
    Debug.Print wb.Name, twb.Name
    If wb.Name <> twb.Name Then
        wb.Sheets(1).Copy after:=twb.Sheets(Worksheets.Count)
        Application.ActiveWorkbook.ActiveSheet.Name = Range("A2").Value
        Application.ActiveWorkbook.ActiveSheet.Range("C:C,D:D,E:E,G:G,I:I,K:K").Delete Shift:=xlToLeft
        wb.Close Savechanges:=False
    End If
x = 2
    Do While Cells(x, 2).Value <> ""
        If Cells(x, 5).Value <> "*Item*" Then
            Rows(x).Delete Shift:=xlUp
            x = x + 1
        End If
    Loop
Next
    'Range("A1").AutoFilter
    'Range("A1").AutoFilter Field:=5, Criteria1:="<>Item", Operator:=xlAnd
Sheet1.Select
Call ToggleEvents(True)
End Sub
 
Upvote 0
Thank you! That fixed the problem that was keeping me from getting to an even bigger learning oportunity. I will keep plugging away.
 
Upvote 0

Forum statistics

Threads
1,215,528
Messages
6,125,342
Members
449,218
Latest member
Excel Master

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