Calling a sub routine from a startup Sub

steveh8204

Board Regular
Joined
Aug 20, 2018
Messages
151
I have created a Sub routine in the 'Workbook Open' section of the 'ThisWorkbook' object to open a custom dialog box when the Worksheet is open ("Do you want to generate a report?"). If the user selects yes the sub goes on to contain all the code needed to generate the report (still within this object). This bit works fine but the problem I've got is when the user selects no from the dialog box but then wants to run the routine it generates later on.

I am unfamiliar with how to re-run this sub routine. As the sub is called Private Sub Workbook_Open there is no way to call that as a macro (via a button or whatever) and if I just put Call (sub name from Module) in the Workbook Open sub it comes up with the following error:

Compile error:
Expected variable or procedure, not module

I have done a workaround by recreating all the code within a module but this doesn't seem very efficicent as I'm duplicating the code. I'm sorry if I haven't explained it very well I'm still getting used to the terminology.

Thanks in advance for any help.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Put the "guts" of your current code in a subroutine in a general module, then call it from your Workbook_Open event code and also call it from your CommandButton event code (or from wherever else you want).
 
Upvote 0
Sort of like this:
Code:
Sub Hello()
'Modified  11/19/2018  6:00:26 AM  EST
MsgBox "Hello"
End Sub

Code:
Private Sub Workbook_Open()
Call Hello
End Sub
 
Upvote 0
Thanks, I thought I'd done that but the problem was with the subroutine name. I was sure the error was because calling subs was done in a different way from here but your post prompted me to think I'd just called it the wrong way so I tried changing the name and it worked.

Thanks
 
Upvote 0
Glad we were able to help you.
Come back here to Mr. Excel next time you need additional assistance.
Thanks, I thought I'd done that but the problem was with the subroutine name. I was sure the error was because calling subs was done in a different way from here but your post prompted me to think I'd just called it the wrong way so I tried changing the name and it worked.

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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