Forum Discussion
connderosa
Sep 13, 2022Copper Contributor
Automatically run a macro when opening a workbook
I can't seem to figure this out, and now I'm afraid I may have done the dreaded: "Important: VBA code cannot be undone, so make sure to test your code on a blank workbook, or a copy of an existin...
- Sep 13, 2022
connderosa you need to open the VBA window (ALT-F11) and then in the project explorer (CTRL-R if it isn't already open) you expand the 'microsoft excel objects' folder and double click the "ThisWorkbook".
You should then get a blank page as shown in the image below and click the left drop down and select 'Workbook'. This should automatically insert a
Private Sub Workbook_Open()
End Sub
Then put the code you want to run between those 2 lines
and in the end make sure you save it as a .xlsm or .xlsb file type
hmmm... I should have opened that link first as it says the same thing but better. lol.
so maybe you need to explain better what your question/need is
TSawyer1226
Oct 28, 2022Copper Contributor
Hey mtarler! Do you know if it is possible to reference a specific code from another sheet within the workbook from the Private Sub Workbook call out? or does it have to have the full code listed on the ThisWorkbook page? I would like to run a Notification Email code that is currently on another sheet every time the workbook is opened but am running into a reference or call out issue when I try to run it through the ThisWorkbook page. Put simply, I don't think Excel knows where I want it to look to successfully run the notify macro. I've tried adjusted the reference in the code itself and am doing something wrong lol so I am hoping there is a way for me just to tell the ThisWorkbook code to run all macros on sheetX and call it good.
mtarler
Oct 28, 2022Silver Contributor
no the code doesn't have to all be in that module. why is the other a Private Sub instead of a Public Sub? it is hard to see what exactly you are doing, you should share the workbook or a sample without any private info. If you can't post the workbook then you can host it in onedrive or the like and share a link. Also you should post this as a new question as this isn't directly related to the above post and you could get additional helpers chime in
- TSawyer1226Oct 28, 2022Copper ContributorOk.