AutoFilter not working on added columns

heathball

Board Regular
Joined
Apr 6, 2017
Messages
135
Office Version
  1. 365
Platform
  1. Windows
Mysteriously, according to how i am reading this code....

This code works perfectly the first time I apply it to a workbook on all sheets.
But, when i add columns to various sheets, then run the code again, the row1 AutoFilter is NOT APPLYING to the additional columns. The original columns autofilter is still there.
There are no blank cells in row 1 (contiguous cells with text)
I assumed.... (Rows"1:1" should be all columns in row 1?, provided there are no blank cells seperating non blank cells in row1?)
But, the 'autofit' IS APPLIED to the new columns.

is this....... "If Not ws.AutoFilterMode"..............somehow causing it to not operate as i would like?
I don't see how the 2nd application would be different from the 1st.


VBA Code:
Option Explicit
Sub filterfreezautofit()
'filterfreezautofit
    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
        Cells.EntireColumn.autofit
    Next ws
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Yes, that's the problem. Try this instead:
VBA Code:
Option Explicit
Sub filterfreezautofit_V2()
'filterfreezautofit
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        If ws.AutoFilterMode Then ws.AutoFilterMode = False
        ws.Rows("1:1").AutoFilter
        With ActiveWindow
            .SplitColumn = 0
            .SplitRow = 1
        End With
        ActiveWindow.FreezePanes = True
        Cells.EntireColumn.AutoFit
    Next ws
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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