Code not executing

Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
239
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()?
 
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.

Do you have your book enabled for macros?
Then try this:

Code:
Private Sub Workbook_Open()
  MsgBox "Hello"
End Sub



IN THISWORKBOOK
Place this macro in the code module for ThisWorkbook. Do the following: Hold down the ALT key and press the F11 key. This will openthe Visual Basic Editor. In the left hand pane, double click on "ThisWorkbook". Copy/paste the macro into the empty window that opens up. Save the workbook as a macro-enabled file, close it and then re-open it.
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hey,

Sorry for late reply, i had vacation.

My workbook is filled with macro's and yes, macro's are enabled :)

Code:
[COLOR=#333333]Private Sub Workbook_Open()[/COLOR]  MsgBox "Hello" [COLOR=#333333]End Sub[/COLOR]

This in thisworkbook works.
When i open the workbook i see the messagebox with "Hello"
 
Upvote 0
Hey,

Sorry for late reply, i had vacation.

My workbook is filled with macro's and yes, macro's are enabled :)

Code:
[COLOR=#333333]Private Sub Workbook_Open()[/COLOR]  MsgBox "Hello" [COLOR=#333333]End Sub[/COLOR]

This in thisworkbook works.
When i open the workbook i see the messagebox with "Hello"


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("Persnlk.xls").Close SaveChanges:=False
Workbooks("VB_Macros.xls").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
Hey again,

It works, noticed the problem is somewhere else in my file.

Made a post about it.

Thanks!
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
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