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.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

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,351
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,351
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...
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,940
Messages
5,856,420
Members
431,812
Latest member
Elastomers

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