need help with VBA code

zami

New Member
Joined
Jul 23, 2005
Messages
45
I need a macro to run automatically when a workbook is saved/closed. Assuming this will need to be set up with VBA code?

I'm running Excel 2000 on XP. Thanks.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Well you probably want to look at the BeforeClose and BeforeSave events of the workbook.

To access the workbook module right click the small XL icon next to File on the main menu and select code.

You can now use the dropdowns to create the code stubs for these events.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

End Sub
This would be where you would put whatever code you want.
 
Upvote 0
much thanks, norie.

forgive my ignorance, but what's the difference between setting the scope for private vs public?
 
Upvote 0
What do you mean?

Are you talking about variable declaration or how the sub's are declared?
 
Upvote 0
I honestly couldn't tell you exactly why the sub's in the workbook module's are declared as Private.

Is it a problem?

It's best not to mess with the way Excel VBA declares event code, it will probably just cause you problems if you do.
 
Upvote 0
not a problem that I'm aware of (yet). I just wondered if declaring private mean it would only work for the author of the workbook and public might work for anyone using the workbook. I'd prefer the latter, so wondered if it made a difference.

thanks for all your assistance.
 
Upvote 0
Private subs

1] Are not visible when using Tools | Macro | Macros

2] Are accessible only to other subs in the module where it's declared.

Use it to restrict unwanted/erroneous running of code.
 
Upvote 0
Private/Public doesn't actually have anything to do with who authored the workbook/code.

Event code in the ThisWorkbook module is for that workbook.
 
Upvote 0

Forum statistics

Threads
1,202,975
Messages
6,052,868
Members
444,606
Latest member
rwmhr

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