Macro Throwing error while running during closing - Sub or Function not Defined - vba

I have three macros which are placed in the sheet 2 of the excel sheet. I want to run the macro whenever the excel sheet is closed. I used the following code,
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Macro1
Macro2
Macro3
End Sub
where the Macro1, Macro2 and Macro3 are macros in sheet2. This code is placed in ThisWorkBook part of the excel sheet. When I execute this I get an error,
Compile Error:
Sub or Function not defined
Suppose the sheet2 has name "Nameofsheet2" , Can anybody help me how to solve this problem? I want the macros in the sheet2 to run whenever the excel sheet is closed.
Thanks

Place your macros code on a Module and it will probably work.

well the simplest way is to call them like this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
SheetX.Macro1
SheetX.Macro2
SheetX.Macro3
End Sub
Where SheetX is the internal code-name of your Sheet2 (this is the name that you see in the Project Window of the VBA IDE, it may not be the same name as shown in the Excel sheet tabs)

If you want to call a macro from a workbook that's not the current one, just use Application.Run [workbook]!MacroName,
i.e. Application.Run "Personal.xlsb!clearImmediateWindow"
If it's stored in another sheet, use Sheet2.MacroName.

Related

How to make vba code to run auto-close only on active workbook?

Here's the scenario. I have multiple excel workbooks that copy and paste data among each other. So the macro works to open.copy.close from one workbook then open.paste.close to another. I am working on creating a function to auto run macro when file is closed.
Here's the issue, when I click macro button in workbook 1, it is supposed to open.copy.close from workbook 2. However, because of the auto run when file is closed function in workbook 2, an error will occur (2 macros cannot run at the same time)Any solution for this? I am looking for a solution to only auto run macro when file is closed IF IT IS AN ACTIVE WORKBOOK. Here is what I have now:
Workbook 1
Sub workbook_beforeclose(cancel As Boolean)
Application.Run "Sheet1.UpdateYellowTabs_Click"
End Sub
Workbook 2
Sub Workbook_BeforeClose(Cancel As Boolean)
Workbook.BeforeClose
Application.Run "Sheet12.UpdateGreen_Click"
End Sub
How do I code it in the workbook code to only make this run only when it's active/closed by a human user and not when open/close by macro?
Thanks!
Well I am not sure to understand your final goal from this, but I can answer the "technical" question. Technically, if you want to check if a given workbook is active: If Application.ActiveWorkbook Is Me,
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not Application.ActiveWorkbook Is Me Then Exit Sub ' <-- add this test
''''''''''''''''''''''''''
' Whatever code goes here
''''''''''''''''''''''''''
End Sub
EDIT
But problem is that invoking wb2.close will make the workbook wb2 the "active" one during the execution of the macro. So this method won't work. Another method is to disable events before closing the workbook; so that the event Workbook_BeforeClose will not fire. After that, you can enable events back if needed. The code looks like this:
' ... open wb2 and do the work with it
Application.EnableEvents = False
wb2.Close False
Application.EnableEvents = True
notice, if you had already disabled events at the beginning of the current macro, which is usually recommended, then this additional code wouldn't be needed.

initialize a workbook to open to the same sheet every time when opened

I have a workbook in excel that has an accumulating number of sheets. I understand the excel document will open to the last sheet you were on. But is their a way to make the workbook open to the same page every time? (it's ok if I need to use VBA) thank you in advance for any help.
Excel's Workbook_Open() event fires too soon in the loading process for some methods or properties of the workbook itself to be reliably used. Just activate the sheet that you want to return to in the BeforeClose event handler.
'In ThisWorkbook
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Worksheets("SheetWhatever").Activate
End Sub
Include a Workbook_Open event within the code module for ThisWorkBook
Private Sub Workbook_Open()
'Instead of "Sheet1", use whichever sheet you want to activate
Worksheets("Sheet1").Activate
End Sub

VBA Select a sheet when the workbook is opened

The following program doesn't work when I open my workbook. What are the possible reasons?
' Select the first sheet when the workbook is opened.
Private Sub Workbook_Open()
Sheet4.Select
Sheet4.Range("B1").Select
End Sub
If you hit alt+F11 to go to the VBA code editor. On the left side, under the file name you will see the different sheets, and whatever modules you might have in there. If you go under the ThisWorkbook module
and place your code there, it will automatically run when you start up the Excel File.
you are using the "Select" Method without an Activating a Sheet First!
Yes, when you have closed your workbook last time, the current sheet will remain in the memory index and when you will again open the same workbook, the pointer search for the most recent sheet used based on the index no.
'Here is the code
Private Sub Workbook_Open()
Sheet4.Activate
Sheet4.Select
Sheet4.Range("B1").Select
End Sub
using "Select Method" without Activating the Parent Object is a Crime. lol
Hope this will help you.

Switch back to one workbook after activating another workbook

My problem is as follows:
I have two files opened - file1.xlsm and file2.xlsm. The first one is active. I want to switch back to the first workbook, every time I activate the second one. I put
Private Sub Workbook_Activate()
Windows("file1.xlsm").Activate
End Sub
in a macro module of the second workbook. It doesn't work. Is there a way to do it? Thanks for any help
You have to place
Private Sub Workbook_Activate()
Workbooks("file1.xlsm").Activate
End Sub
in the ThisWorkbook code window of file2.xlsm (not a macro module).
Try
Call Workbooks("filename.xlsm").Activate

Macro from on open event for worksheet

I have a macro that I want to run as part of open event. I recorded the macro to copy and paste some columns on a worksheet. This macro will only work when I am on that worksheet.
Since I want to run this macro as one of many applications when workbook is open and it doesn't always open on this specific worksheet I would like the code to target the worksheet and run the macro.
Here is my code but for some reason it doesn't work :
Application.Run "'Workbook.xlsb'!Sheet5.Copy_Paste2"
If I'm understanding you, you need to call Copy_Paste2 from ThisWorkbook Open event.
Private Sub Workbook_Open()
Sheet5.Copy_Paste2
End Sub