Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute Macro when spreadsheet Opened (Excel VBA)

Status
Not open for further replies.

artsandcraftshome

Technical User
Mar 24, 2003
13
US
I have a spreadsheet with a macro that I have created a custom button for, but would like to execute the macro when the spreadsheet is opened. Then I'd like to be able to re-execute it (if necessary) using the button. Is this possible?
thanks
John
 
You can do that quite easily. Just keep track of which code page does what. From the VBA editor, put your macro in a code module:
[blue]
Code:
Option Explicit

Sub DoSomething()
  MsgBox "Doing something"
End Sub
[/color]

Then on the sheet code page have this which runs the macro when the button is clicked:
[blue]
Code:
Option Explicit

Private Sub CommandButton1_Click()
  DoSomething
End Sub
[/color]

and on the "ThisWorkbook" code page have this which runs the macro when the workbook is opened:
[blue]
Code:
Option Explicit

Private Sub Workbook_Open()
  DoSomething
End Sub
[/color]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top