I am trying to get this macro to loop through all sheets within a workbook & then filter & clear some cells.. the loop works when I do not have the filter/clear cells part of the macro enabled however as soon as I uncomment the filter & clear part of the macro, the macro will only filter and clear the cells within the Active Sheet, any ideas where I am going wrong?
Any help very much appreciated!
Sub MovethroughWB()
Dim ws As Worksheet
For Each ws In Sheets 'This statement starts the loop
If ws.Name <> "Exclusions" Then 'Exclude this sheet from the loop
Range("D2:J2").Select
Selection.AutoFilter
ActiveSheet.Range("$D$2:$J$200").AutoFilter Field:=7, Criteria1:="<>#N/A", _
Operator:=xlAnd
Range("D3:J200").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.ClearContents
End If
Next ws
End Sub
Referce a worksheet for every object that is located in a worksheet ( Range
, Cells
, Columns
, Rows
, etc)
Stop using .Select
( How to avoid using Select in Excel VBA ).
And your code gets reliable:
Sub MovethroughWB()
Dim ws As Worksheet
For Each ws In Sheets 'This statement starts the loop
If ws.Name <> "Exclusions" Then 'Exclude this sheet from the loop
ws.Range("D2:J2").AutoFilter
ws.Range("$D$2:$J$200").AutoFilter Field:=7, Criteria1:="<>#N/A", Operator:=xlAnd
ws.Range("D3:J200").SpecialCells(xlCellTypeVisible).ClearContents
End If
Next ws
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.