apply to all sheets in active workbook

heathball

Board Regular
Joined
Apr 6, 2017
Messages
133
Office Version
  1. 365
Platform
  1. Windows
I have this tiny issue where i am trying to apply the 2nd part of this code (auto filter & Freeze panes)
to all sheets in the active workbook.

Does anyone know what change is required to achieve that?

VBA Code:
Sub filterfreezeallsheets()
Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate


Rows("1:1").Select
    Selection.AutoFilter
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Normally, the method would look something like this:
VBA Code:
Sub filterfreezeallsheets()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
   
    'Your code goes here

    Next ws
End Sub
 
Upvote 1
Thanks Kevin. I have applied this code.
It goes through the process of appearing to apply to all sheets (its busy and moving like it should)
But it is only working on the active sheet?


VBA Code:
Sub filterfreezeall()

    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
   
    Rows("1:1").Select
    Selection.AutoFilter
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True

    Next ws
End Sub
 
Upvote 0
Because you're using With ActiveWindow - which means the code will only be applied to whatever window is active. The following will apply the autofilter to all sheets, but I can't quite figure out which cell you want to apply the freeze panes to ?!
VBA Code:
Sub filterfreezeall()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        With ws
            .Rows("1:1").AutoFilter
        End With
    Next ws
End Sub
 
Upvote 1
Because you're using With ActiveWindow - which means the code will only be applied to whatever window is active. The following will apply the autofilter to all sheets, but I can't quite figure out which cell you want to apply the freeze panes to ?!
VBA Code:
Sub filterfreezeall()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        With ws
            .Rows("1:1").AutoFilter
        End With
    Next ws
End Sub
ok i understand.
how do i add "freeze top row" to all sheets?
 
Upvote 0
Looks like you have to activate the sheet & use active window after all...
VBA Code:
Option Explicit
Sub filterfreezeall()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        If Not ws.AutoFilterMode Then ws.Rows("1:1").AutoFilter
        With ActiveWindow
            .SplitColumn = 0
            .SplitRow = 1
        End With
        ActiveWindow.FreezePanes = True
    Next ws
End Sub
 
Upvote 1
Solution
wow. thats awesome. thanks.
it also works when some sheets have it applied, and some not (instead of reversing it on sheets where it has been applied) - which is great

with "option explicit" i'm reading about it-
I cannot find a clear reason why you chose to add it to this code.

"finding typing errors" or/and "force yourself to declare all the variables" is about all i can see so far.
 
Upvote 0
Happy to help and thanks for the feedback (y) :)
why you chose to add it to this code.
Just a habit that's good to get into. It can save you a lot of problems in the long run, and doesn't hurt to include it every time.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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