Looping through worksheets not working

sjk1193

New Member
Joined
Nov 12, 2018
Messages
29
I have the code below and seems pretty standard but it only runs for 1 worksheet and doesnt loop through them all. Cant figure out whats worng

VBA Code:
Sub formatdata2()

Dim keepcolumn As Variant
Dim fnd As Variant
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
        'Cells(1, 1).Select
    
        LastCol = Cells(1, 1).End(xlToRight).Column
        For j = LastCol To 1 Step -1
            keepcolumn = 0
            If Cells(1, j).Value = "Number" Then keepcolumn = 1
            If Cells(1, j).Value = "Name" Then keepcolumn = 1
            If Cells(1, j).Value = "Base" Then keepcolumn = 1
            If Cells(1, j).Value = "Start Date" Then keepcolumn = 1
            If Cells(1, j).Value = "End Date" Then keepcolumn = 1
            
            If keepcolumn = 0 Then
                Cells(1, j).EntireColumn.Delete
            End If
        Next j
    
        'deletes rows with clasification level = 2
        LastRow = Cells(Rows.Count, 1).End(xlUp).row
        Set fnd = ws.Range("1:1").Find("Which Classify Level", , , xlWhole, , , False, , False)
        
        For i = LastRow To 1 Step -1
            If Cells(i, fnd.Column) = "2" Then
                Cells(i, fnd.Column).EntireRow.Delete
            End If
        Next i
Next ws

End Sub
 

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.
This line:
Rich (BB code):
For Each ws In ThisWorkbook.Worksheets
will loop through the worksheets, but doesn't actually select or activate them.

So if you have range references in that loop with no sheet reference, they will keep referring to whatever the active sheet is at that time.

You need to either add sheet references in to all of your range references in that loop, or just select/activate that sheet first, i.e.
Rich (BB code):
For Each ws In ThisWorkbook.Worksheets
    ws.Activate
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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