2016 Not Loop Through Sheets

Snowshoeken

Active Member
Joined
Aug 8, 2002
Messages
306
I cannot get my new Excel 2016 to loop through worksheets. Just runs the code the same number of times there are sheets, but not on the sheets.

Code:
Sub Don_StepALL()
'
' Macro1 Macro
'

'
    Dim ws As Worksheet
    For Each ws In Sheets
    Range("K2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(AND(RC[-10]=""9740"",RC[-6]=""N/A""),""NON RURAL"",RC[-6])"
    Selection.AutoFill Destination:=Range("K2:K65536")
    Range("K2:K65536").Select
    Selection.Copy
    Range("E2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Columns("K:K").Select
    Application.CutCopyMode = False
    Selection.ClearContents
    Columns("E:E").Select
    Selection.Replace What:="0", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
    Range("A1").AutoFilter Field:=8, Criteria1:="<>SOFT_BROADSOFT" ', Operator:=xlAnd
    Range("A2:J" & Range("J2").End(xlDown).Row).EntireRow.Delete
    ActiveSheet.ShowAllData
    
    Range("A1").AutoFilter Field:=5, Criteria1:="N/A" ', Operator:=xlAnd
    Range("A2:J" & Range("J2").End(xlDown).Row).EntireRow.Delete
    ActiveSheet.ShowAllData

     Next ws
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
If you do not select each worksheet, or use worksheet references in your range references, you will never move off your first page (looping through all the worksheet does not actually select or activate them!).

The easy fix is to do this:
Code:
Dim ws As Worksheet
    For Each ws In Sheets
[COLOR=#ff0000]    ws.Activate[/COLOR]
    'rest of your code here
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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