Auto filter a column when file is opened

russelldt

Board Regular
Joined
Feb 27, 2021
Messages
158
Office Version
  1. 365
Platform
  1. MacOS
I have a file that has a set value on Sheet 1 Cell A1.

Sheet 2 contains a table of data that is updated from a separate file, so each time the first file is opened, the data is updated.
Row 3 of sheet 2 contains a list of table column headings. One column (3) needs to be filtered to only show the value on Sheet1, Cell A1. It's a simple Filter operation (clear filter and filter again) to update Sheet 2's values.

I want to find out if this filter operation can be performed automatically each time i open the file - either with a Macro or VB script.

Thanks
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try this in the ThisWorkbook module

1658891246410.png


VBA Code:
Private Sub Workbook_Open()
  With Sheets("Sheet2")
    .AutoFilterMode = False
    .Range("C3", .Range("C" & Rows.Count).End(xlUp)).AutoFilter Field:=1, Criteria1:=Sheets("Sheet1").Range("A1").Value
  End With
End Sub
 
Upvote 0
Try this in the ThisWorkbook module

View attachment 70195

VBA Code:
Private Sub Workbook_Open()
  With Sheets("Sheet2")
    .AutoFilterMode = False
    .Range("C3", .Range("C" & Rows.Count).End(xlUp)).AutoFilter Field:=1, Criteria1:=Sheets("Sheet1").Range("A1").Value
  End With
End Sub
Thanks Peter. I get a run time error on the second line. I have tried "SheetInvoice log" as well (without the space) and i get the same error message.


Private Sub Workbook_Open()
With Sheets("Sheet Invoice log")
.AutoFilterMode = False
.Range("C3", .Range("C" & Rows.Count).End(xlUp)).AutoFilter Field:=1, Criteria1:=Sheets("Sheet Cashflow input").Range("E3").Value
End With
End Sub
 
Upvote 0
Thanks Peter. I get a run time error on the second line. I have tried "SheetInvoice log" as well (without the space) and i get the same error message.


Private Sub Workbook_Open()
With Sheets("Sheet Invoice log")
.AutoFilterMode = False
.Range("C3", .Range("C" & Rows.Count).End(xlUp)).AutoFilter Field:=1, Criteria1:=Sheets("Sheet Cashflow input").Range("E3").Value
End With
End Sub
My error, I forgot to remove the "Sheet" from the script.

Works like a charm.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,925
Members
449,094
Latest member
teemeren

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