Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
233
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I use this code to filter data in my workbook;

Code:
Private Sub Workbook_Open()On Error GoTo Errorhandler
Workbooks("Persnlk.xls").Close SaveChanges:=False
Workbooks("VB_Macros.xls").Close SaveChanges:=False


Worksheets("Shifts").AutoFilterMode = False


Worksheets("Shifts").Range("J1:Q" & Range("J" & Rows.Count).End(xlUp).Row).ClearContents


Worksheets("Shifts").Range("A1").AutoFilter Field:=2, Criteria1:="<>"
Worksheets("Shifts").Range("A1").AutoFilter Field:=3, Criteria1:="<>"


Worksheets("Shifts").AutoFilter.Range.Copy


Worksheets("Shifts").Range("J1").PasteSpecial xlPasteValues


Application.CutCopyMode = False


Worksheets("Shifts").AutoFilterMode = False


Exit Sub
Errorhandler:
End Sub

I notice that this code is not working under Workbook_Open()

When i paste it in a module and press on an button it works.

I would like to make this code work when i open the workbook. Anyone knows why it's not working under workbook_Open()?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I think you have placed the code in the wrong module. Go into the VBA editor, look to the left side and find the window titled Project-VBAProject... it has a list of the sheets in your workbook... look at the bottom of that list for the entry labeled ThisWorkbook... double click it and place your Workbook_Open code procedure in the code window that just opened up (make sure to remove that code from any other modules you put it in). That's it... save your workbook and the next time you open it, your code should run.
 
Upvote 0
On top of what Rick has said, if these two workbooks
Code:
Workbooks("Persnlk.xls").Close SaveChanges:=False
Workbooks("VB_Macros.xls").Close SaveChanges:=False
are not open when the code runs. Then nothing will happen
 
Upvote 0
You can put the code in a normal module, but the macro should be called "Auto_Open".
Make the following changes:

Code:
Sub Auto_Open()
[COLOR=#ff0000]    On Error Resume Next[/COLOR]
    Workbooks("Persnlk.xls").Close SaveChanges:=False
    Workbooks("VB_Macros.xls").Close SaveChanges:=False
[COLOR=#ff0000]    On Error GoTo 0[/COLOR]
[COLOR=#ff0000]    If Worksheets("Shifts").AutoFilterMode Then Worksheets("Shifts").AutoFilterMode = False[/COLOR]
    Worksheets("Shifts").Range("J1:Q" & Range("J" & Rows.Count).End(xlUp).Row).ClearContents
    Worksheets("Shifts").Range("A1").AutoFilter Field:=2, Criteria1:="<>"
    Worksheets("Shifts").Range("A1").AutoFilter Field:=3, Criteria1:="<>"
    Worksheets("Shifts").AutoFilter.Range.Copy
    Worksheets("Shifts").Range("J1").PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    Worksheets("Shifts").AutoFilterMode = False
End Sub


If an error occurs, write here the error message and the macro line where it stopped.
 
Upvote 0
You can put the code in a normal module, but the macro should be called "Auto_Open".
Make the following changes:

Code:
Sub Auto_Open()
[COLOR=#ff0000]    On Error Resume Next[/COLOR]
    Workbooks("Persnlk.xls").Close SaveChanges:=False
    Workbooks("VB_Macros.xls").Close SaveChanges:=False
[COLOR=#ff0000]    On Error GoTo 0[/COLOR]
[COLOR=#ff0000]    If Worksheets("Shifts").AutoFilterMode Then Worksheets("Shifts").AutoFilterMode = False[/COLOR]
    Worksheets("Shifts").Range("J1:Q" & Range("J" & Rows.Count).End(xlUp).Row).ClearContents
    Worksheets("Shifts").Range("A1").AutoFilter Field:=2, Criteria1:="<>"
    Worksheets("Shifts").Range("A1").AutoFilter Field:=3, Criteria1:="<>"
    Worksheets("Shifts").AutoFilter.Range.Copy
    Worksheets("Shifts").Range("J1").PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    Worksheets("Shifts").AutoFilterMode = False
End Sub


If an error occurs, write here the error message and the macro line where it stopped.

Hey,

How do i run this module when i open the workbook?
 
Last edited:
Upvote 0
On top of what Rick has said, if these two workbooks
Code:
Workbooks("Persnlk.xls").Close SaveChanges:=False
Workbooks("VB_Macros.xls").Close SaveChanges:=False
are not open when the code runs. Then nothing will happen

The 2 workbooks are closed yes. That explains why nothing happens then
 
Upvote 0
It doesn't run.

Code:
Workbooks("Persnlk.xls").Close SaveChanges:=False
    Workbooks("VB_Macros.xls").Close SaveChanges:=False

This doesn't work for example. The files stay open.
This makes me think the rest also doesn't work.

When i place the module under a button and i press it, it works.
 
Last edited:
Upvote 0
It doesn't run.

Code:
Workbooks("Persnlk.xls").Close SaveChanges:=False
    Workbooks("VB_Macros.xls").Close SaveChanges:=False

This doesn't work for example. The files stay open.
This makes me think the rest also doesn't work.

When i place the module under a button and i press it, it works.

Try this, close de book an reopen.

Code:
Sub Auto_Open()
  MsgBox "hi, I am running!"
End Sub


If the books are open and you want to close them, they must be in the same excel application. Check the names of books and extensions.


Now try this:

Code:
Sub Auto_Open()
    
    Workbooks("[COLOR=#ff0000]Persnlk.xls[/COLOR]").Close SaveChanges:=False
    Workbooks("[COLOR=#ff0000]VB_Macros.xls[/COLOR]").Close SaveChanges:=False
    
    If Worksheets("Shifts").AutoFilterMode Then Worksheets("Shifts").AutoFilterMode = False
    Worksheets("Shifts").Range("J1:Q" & Range("J" & Rows.Count).End(xlUp).Row).ClearContents
    Worksheets("Shifts").Range("A1").AutoFilter Field:=2, Criteria1:="<>"
    Worksheets("Shifts").Range("A1").AutoFilter Field:=3, Criteria1:="<>"
    Worksheets("Shifts").AutoFilter.Range.Copy
    Worksheets("Shifts").Range("J1").PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    Worksheets("Shifts").AutoFilterMode = False
End Sub

If an error occurs, write here the error message and the macro line where it stopped.
 
Upvote 0
I tried, no messagebox is showing.

Maybe i made the module wrong?

I just did Insert -> Module and changed it's name to Auto_Open. Nothing more nothing less.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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