Excel::VBA - How to reset the 'OnXXX' macros for worksheets - excel

I have an old workbook (made by someone years ago) with a few worksheets in it. When I open this workbook, Excel complains with messages like 'Cannot find ActivateWorksheet', 'Cannot find DeActivateWorksheet'.
There are no event handlers in the code. I want to avoid getting these messages from Excel but could not find how to reset them. I checked the OnSheetActivate, OnSheetDeactivate etc properties to see if some macro is assigned but found them to be empty.
Is there any other place where I can check and remove these handlers?

It might be related to AddIns - do you have all the necessary AddIns installed?

There's one more place to look: NamedRanges. I found that the worksheet has Names defined with values such as "=ActivateWorksheet", "=DeActivateWorksheet" but these macros were nowhere. When a worksheet is activated, the Names on that sheet is refreshed (or recalculated) and i my case the macro were being called.

Related

Method 'Add' of object 'Sheets' failed, and cannot rename new sheets

In my VBA project, I frequently make a temporary sheet in the workbook, which gets deleted before the macro exits. The sheet is added as follows
temp = "Temp"
ThisWorkbook.Sheets.Add(after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = temp
This has been going well for quite a while. Now, however, the code returns an error
Run-time error '1004': Method 'Add' of object 'Sheets' failed
A sheet has been added, although not named "Temp" (instead it's named Sheetn).
I can successfully add a new sheet, but get the following error message. I am unable to change the name of the newly added sheet.
"We couldn't move this sheet"
The workbook is not protected, and there is no other sheet named Temp present in the workbook. Because of this, I feel I have encountered an invisible wall that prevents users from adding large quantities of sheets, even though these sheets are deleted.
This puzzles me, any explanations and/or workarounds?
Having noted from the comment thread that you've solved it by Saving, Closing and Reopening, that #SandPiper has had the same problem recur after a while, and that your OP asks for workarounds, the obvious one that I would suggest would be to stop creating and then removing the Temp sheet, and instead simply create it as a permanent part of the workbook, then hide it, and simply use it when it's needed and clear it afterwards (programmatically).
Having the same problem here and in my case the problem was resolved by temporarily disabling the autosave function. I never had this problem until i started working with 365 version. Any other having this problem on other version then 365?
Microsoft® Excel® voor Microsoft 365 MSO (Versie 2202 Build 16.0.14931.20128) 64 bits

Running Excel Add-In Macro Based on Workbook Events

I want to create a macro that takes the user to the last previously active sheet. I need this macro to keep track of all previously active sheets, which it should be able to do automatically. I want to add this to an Excel add-in, since it needs to be compatible with all the worksheets that my company's using. The sheets themselves are not macro-enabled, so I can't have any code stored in the modules of ThisWorkbook.
I have tried out the examples that I've found on other websites. Unfortunately, all of them require using ThisWorkbook, which I won't be able to use. Is there a way to do this purely within an Excel add-in?
Public MyPrevSheet as String
Sub GoToPreviousSheet()
If Len(MyPrevSheet) > 0 Then
Sheets(MyPrevSheet).Activate
ActiveWorkbook.ActiveSheet.Previous
Else
MsgBox "You have not switched sheets yet since opening the file!"
End If
End Sub
Ideally, I would like for MyPrevSheet to automatically track the last active sheet in the file.
Update: I managed to make it work using Application Events. The idea is to create a class module that can then be assigned the Application.
Here are some useful links that helped me: http://www.cpearson.com/excel/AppEvent.aspx
https://www.jkp-ads.com/Articles/buildexceladdin05.asp

Error when setting workbook variable - 2147352565

I have a simple code that runs upon initialization of a userform that sets a few workbook and worksheet variables so they can be used easily throughout the rest of my modules, and the references can be easily changed in one place if the file moves. I recently migrated my workbooks from my desktop to a separate server/drive, and accordingly updated the file pathways; however, when I try to run the code now I immediately get the message:
"Run-time error '-2147352565 (8002000b)': Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus."
This error occurs on the line
Set ReportWkbk = Workbooks("N:\ rest of file pathway here\QuaRT_Template.xlsm")
Is there anything that can be happening on the server/drive that is causing this issue? If so, is there anything I can do to fix it? I do work with other excel workbooks saved in the same location that seem to have no issue being referenced, though their references are in the workbook itself, not through Visual Basic.
The Subscript Out of Range error occurs because the Excel workbook being referenced is not open (or opening) in the same instance of Excel. You can easily reproduce the error by creating two workbooks (name one WorkBook2.xlsx) and ensuring they open in separate instances of Excel. Then run this code:
Sub OpenWkbkNames()
Dim wbk As Workbook
For Each wbk In Workbooks
Debug.Print wbk.Name
Next
'Hmm. Workbook 2 is not listed, but lets activate it and see what happens.
Workbooks("workbook2.xlsx").Activate
End Sub
You could avoid the issue by looping through the names of the workbooks open in THIS instance of Excel to ensure its available.
I've been researching Run-time error '-2147352565 (8002000b)' and found this: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vsconstants.disp_e_badindex.aspx
Apparently, that is VS's way of saying Subscript Out of Range too. I suspect (but have no solid proof) that since the code worked locally but the issue appeared after the file was migrated to a shared drive that Windows is taking a long time to open the file so opens it in a new instance of Excel. Essentially, Excel gets impatient and decides to move on while Windows completes its task. I base this assumption on the detection logic that was added to Excel 2013 (https://blogs.office.com/2013/06/03/opening-workbooks-by-running-separate-instances-of-excel/).

Excel/VBA workbook.activesheet returns wrong sheet

I have a workbook (we'll call it "AAA") that is based on a template and refers to code modules in a second workbook. The second workbook (we'll call it "CodeStorage") is a repository for code modules so that any saved versions of the template will be able to access modified/updates code without a change to the saved workbook.
My problem arises when I have multiple windows open in "AAA" and try to get the activesheet when a module is running in "CodeStorage". In code, I create an object ("oWorkbook") that is a reference to the workbook "AAA" When "AAA" has focus, oWorkbook.Activesheet returns the sheet for the active window. When "CodeStorage" is running a module and thus has focus, oWorkbook.Activesheet returns the sheet that is selected in window #1 regardless of what window (#2, 3, etc) was active when the code module in "CodeStorage" was called.
Has anyone run into this and have you found a work around?
ActiveSheet is confusing the way you are using it.
You need to explicitly activate a sheet for it to be considered the ActiveSheet. Running code in another workbook does not activate it.
Selecting cells in a worksheet will activate it. Or specifically calling Activate.
You could do something like:
oWorkbook.Activate
oWorkbook.Activesheet
Alternatively, and preferably, you could do something like the following:
oWorkbook.Worksheets("Sheet1")
oWorkbook.Worksheets(1)
Both these are better. If your user selects a different workbook during runtime execution or you select something in a different sheet, ActiveSheet will return something different.
It's better to fully qualify you workbook paths when using multiple workbooks. This will save you a ton of headache in the future for dealing with "what is activated?" or "what is selected?" This answer is worth reading, too.
To rephrase my question, I was looking for a way to reference the last active worksheet in a workbook that had multiple windows open. If window 1 was selected, I wanted the sheet from window 1, same for window 2, 3, or ???. I also needed the reference when a code module in a different workbook was running and the code module had a variable that was an object reference to the calling workbook.
The solution is Workbook.Windows.Item(1).ActiveSheetView.Sheet. When called from the running code module, even in a different workbook, it returns the same as workbook.activesheet when that is called from the workbook itself. My tests show that Workbook.Windows.Item(1) is the last active window when a workbook loses focus.
Thank you enderland. You got me pointed in the right direction.

Custom Excel VBA Hotkey for "Go To Previous Sheet"

I am attempting to make a custom hotkey that will go to the previous sheet viewed.
As far as I understand, this would involve making a Class Module as a Worksheet object that will capture Sheet Change, Sheet Activate, and Sheet Deactivate Events. Once the event has been captured, I would set the previous active worksheet to an object variable. Then I would make a custom macro that would go to the previously set worksheet object once a hotkey is pressed. Is this the right path?
And if it is the right path, where would I save the Macro code so that it's usable by all future workbooks? I have a PERSONAL.xlsb file created and ready to edit. Would I make the class module in PERSONAL.xlsb? How would I initialize the object in PERSONAL.xlsb when I create a new workbook?
Answers to these questions would be greatly appreciated, thank you!
Generally on the right line, but:
you only need the Deactivate event to capture the last sheet
to code this for all workbooks, you would use an Application level event handler. See cPearson site
you may want to handle WorkbookDeactivate as well
I would create a class event handler (as described in the link) including a property for the last sheet, and a Module level Sub to get the LastSheet from the class, and activate it

Resources