Open Second Workbook Triggers VBA Code in First Workbook - excel

I open an Excel workbook which has some VBA code in it including Event code on one of the worksheets. I open a second workbook which has no VBA code in it at all but as soon as the second workbook opens, it triggers the VBA code to run in the first workbook. The code fails because the first workbook is not the Active Workbook. Anybody got any ideas why opening a second workbook should trigger event code in the first workbook?

I can replicate that -seems like opening a workbook triggers the calculate event in other open workbooks. Having said that, your event-handling code should not rely on any particular workbook being active when it runs, so it would help to post that if you want suggestions for fixes.
For example the worksheet object which corresponds to the sheet module can be referenced via Me, and the workbook containing that sheet is Me.Parent
Private Sub Worksheet_Calculate()
Debug.print "calculating " & Me.Name & " in " Me.Parent.Name
End Sub

Related

Screen Updating and Workbook.Activate not working as expected

I work with a a group of six interlinked Excel workbooks that all feed into one summary/reporting workbook (let's call it "Main Report.xlsm").
Since the various workbooks are updated with new information throughout the day as it comes to hand, they need to be saved regularly throughout the day. I have therefore created a simple VBA macro that saves all open workbooks at the press of a button, which I have located as on object on Sheet1 of MainReport.xlsm. The macro works OK and saves all open workbooks as expected, however two aspects of the code are not working as expected:
No matter where I insert ScrenUpdating = FALSE, Excel still displays each workbook on the screen as it saves it. I don't want that because it slows down the save process, plus it looks weird as the macro toggels through each open workbook and the workbooks flash up momentarily on the screen. Since I activate the save macro from Main Report.xlsm, I want the screen to freeze on that workbook whilst Excel saves all open workbooks as a 'background' process. Can anyone tell me how I can make ScreenUpdating work properly with this code?
At the end the macro is supposed to select the 'Main Report.xlsm' workbook, but instead of activating that workbook, Excel seems to get stuck on displaying the second-last workbook in the group of six open workbooks. If I test the code by stepping through it, it seems to work OK, but when I run it from the assigned button on Main Report.xlsm, it does not work. Why would this be happening?
Here is my simple 'SaveAll' macro VBA code:
Sub SaveAll()
Application.ScreenUpdating = False
Dim Wkb As Workbook
Application.Calculate
For Each Wkb In Workbooks
If Not Wkb.ReadOnly And Windows(Wkb.Name).Visible Then
Wkb.Save
End If
Next
Application.Workbooks("Main Report.xlsm").Activate
ActiveWorkbook.Worksheets("Sheet1").Select
ActiveSheet.Range("C4").Select
Application.ScreenUpdating = True
End Sub
Can anyone spot what I am doing wrong?

Copy data range from macro free workbook to another macro free workbook

I am new to VBA!
I have a workbook A that I use as a template for spinoff workbooks B, C, D, etc.
I made an error in formulas range A36:E37. I need to correct it in all the subsequently created workbooks, which can have any random name
I want to open the corrected master workbook A, and copy range from A to whateverworkbookname
Every time I use thisworkbook refrerence, it pastes the data to my personal macro workbook, same thing with activeworkbook.
I'm sure there's a simple solution, (like assigning a variable to the freshly opened workbook that needs fixing?) but I don't know how to do that.
Help is much appreciated!
Also of note, I am planning on manually opening the whaverworkbookname, then VBA unprotecting the sheet, copy paste function, protecting the sheet, saving, and closing the whateverworkbookname book when the macro completes, to be repeated with the rest of the incorrect workbooks.
If there is a smarter way to do this (which is probably way over my head) like applying a macro to all workbooks in a folder for instance, I would be interested in a point in the right direction to learn about it.
First a note:
ThisWorkbook always refers to the workbook the code is written in. This never changes.
ActiveWorkbook is the workbook that has focus (is on top). This changes easily with a mouse click on any workbook.
The issue is probably that you run the code from VBA editor. But if it is in your personal workbook and you run from the editor, then as soon as you are in the code your active workbook is the personal one because that is where your code is and if you click there to run the code it has focus.
You can check if the active workbook is the personal one
If ActiveWorkbook.Name = ThisWorkbook.Name Then
MsgBox "The active workbook is the personal one. Make sure to focus on the correct workbook."
Exit Sub
End If
'rest of your code
Create a button or link a ribbon button to launch your macro and use ActiveWorkbook in your code.

How to activate the excel from where I run my vba but not by Windows(filename).activate

I have an Excel workbook called Book1 with VBA code that opens another workbook and gets data from it to add it to Book1.
In my code it is written that:
Windows("Book1.xlsm").Activate
When VBA gets back to the workbook where my code is starting, I want to make a reference to my workbook so that it won't matter whether the workbook will be named Book2 or any other name and won't matter where it will be located. How can I do this please?

How to Activate a specific Workbook on non-VBA Excel Quit

I have a workbook ("CodeBook.xlsm") that runs code using a BeforeSave event. When a user has multiple workbooks open and chooses to quit excel via File/Exit, the user is prompted whether to save workbooks, and if yes to CodeBook.xlsm, then the BeforeSave code is run. The problem is, at that point the ActiveWorkbook may not be CodeBook.xlsm, unless that happens to be the workbook that the user was in when he/she selected Exit Excel. If the user quit excel from another workbook, the BeforeSave code is running but the activeworkbook is some random file of the user, so all the references to specific worksheets and ranges in the BeforeSave code do not work.
I have tried various ways using a Static declaration to retain the name of CodeBook and workbook().activate to activate it when the application is quitting, but when BeforeSave runs, it can't pick up the name CodeBook anywhere, short of hard-coding the name into the code.
Any suggestions? How to retain a variable name in memory when there is no code running, but is there when a user initiates a quit excel, OR how to activate a specific workbook when Excel is quitting from a user command and not from application.quit. Using excel 2010.
I overcame this by including a reference to the specific workbook.
For example, this code simply saves the date/time stamp in cell A1 of Sheet1 before closing the document. By adding ThisWorkbook, it works on the specific workbook that the code resides in. If you don't add ThisWorkbook then it will work on the active workbook when the user quits.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Now()
End Sub
Place this code in the ThisWorkbook module.

sub terminates but desired workbook not displayed

I have a VBA program that asks for the user to enter a desired range, which is in another opened workbook. If there is an error, i.e. the other workbook is manually activated, but no range is selected, or there is an error, I want the program to display the original macro workbook sheet with an error message. The code below works in Debug mode, but when the VBA program is run, it displays the error correctly but does not display the original macro worksheet. It remains on the sheet that was manually activated by the user. What am I missing?
In the code below, "HMArea" is a Range variable returned by the routine getting user input.
"Macro_Fname" is a string variable for the file name of the original VBA program.
HM_file = FileName(HMArea)
If HM_file = "Macro_Fname" Then
Windows("Macro_Fname").Activate
Sheets("[name of the sheet in Macro_Fname]").Select
Range("D4").Select
MsgBox "ERROR: No data selected"
Exit Sub
End If
Try changing
Windows("Macro_Fname").Activate
to
Workbooks("Macro_Fname").Activate
However, the exact nature of your question is vague. Assuming you are in Workbook A, do you want to select a range in Workbook B (which is open at the same time)?
To refer to macro workbook use ThisWorkbook
When another workbook is opened always assign it to variable so that you can have control over it.
Set wbk = Workbooks.Open("D:\test.xlsx")
When working with multiple workbook always prefix the workbook object. If its ignored it will take active workbook.
`Sheets("[name of the sheet in Macro_Fname]").Select`
Avoid using Select/Acitvate. See here
Range("D4").Select
Once the above issues are fixed your code will run as expected.

Resources