Calling Workbook_Open() marco using a button.

bposaner

Board Regular
Joined
May 28, 2002
Messages
74
I got a Private Sub Workbook_Open() marco which runs fine and refreshes various listboxes.
However once opened I need to refresh various sheets, linked to access databases. This works fine, but then the list boxes could be wrong. So i need to rerun the Workbook_Open macro.
What i'm trying to do is add a button which will run the "Private Sub Workbook_Open()" macro.
The only way I can run the macro is open VBA, and manually run the macro.

I tried createing a macro and calling "Workbook_Open()" but it wasn't any good.

So your help would be great.
 

Some videos you may like

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Lewiy

Well-known Member
Joined
Jan 5, 2007
Messages
4,284
The Workbook_Open event cannot be called from anywhere else other than by opening the Workbook as it is a Private Event.

What I suggest you do is put your code in a Module and call it something like:
Sub OpenMacro()

Then under the Workbook_Open event, call this macro, i.e:
Code:
Private Sub Workbook_Open()
OpenMacro
End Sub

You can then also call that macro from a command button, i.e:
Code:
Private Sub CommandButton_Click()
OpenMacro
End Sub
 

bposaner

Board Regular
Joined
May 28, 2002
Messages
74
I had thought of that.
Just means more work to change the several workbooks I use this for :(
Was hoping for a simple answer.
Thanks.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,235
Office Version
  1. 365
Platform
  1. Windows
Why not just remove the Private?

Then you can use something like this.
Code:
ThisWorkbook.Workbook_Open
 

bposaner

Board Regular
Joined
May 28, 2002
Messages
74

ADVERTISEMENT

So will the macro still automatically run when the workbook opens?
And I will also be able to call it from a button?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,235
Office Version
  1. 365
Platform
  1. Windows
Yes it will, well it did when I tested it.:)

The only disadvantage is that the workbook Open event will now be listed when you goto Tools>Macros...
 

Watch MrExcel Video

Forum statistics

Threads
1,123,489
Messages
5,601,986
Members
414,489
Latest member
Xlambda

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
Top