Excel Crashes Upon Editing Script in ThisWorkbook - vba

I have an Excel 2010 file (.xlsm) that contains code in a few modules including ThisWorkbook. The ThisWorkbook module contains a few Event procedures, including a _SheetActivate event.
I have a button on one sheet that, when clicked, activates a different sheet.
Today after working on some code in one of the regular modules, I clicked the button and got a
Run time error '-2147417848 (80010108)'
Method 'Activate' of object '_Worksheet' failed.
Any attempt to activate a different sheet (manually or programmatically) crashes the Application.
After some extensive research, all I found was this page which gives further information regarding the error:
-2147417848 (80010108): The object invoked has disconnected from its clients.
Resolving the error isn't so much of a concern to me because I don't need that particular event anymore. However, I do need to figure out why Excel keeps crashing. I tried to delete the code, but Excel crashed. So I tried commenting the code out, but Excel still crashed. Alas, everything I've tried has resulted in a crash. This leads me to believe that something must be corrupt in the script within that module (the code in other modules seems to work fine, but none of the other procedures activate a sheet).
I've read that an option would be to copy everything (including code) over to a new workbook, but I'd like to avoid that if possible as that would require copying more than the current sheet...but I can't copy the other sheets without Excel crashing. Does anyone have any ideas on how I could remove/delete the code from ThisWorkbook?

After too many crashes to count, I was finally able to resolve the issue.
First I copied the code (Ctrl + C) from ThisWorkbook that I wanted to be able to use later and pasted it into a blank notebook file. Then I did as Patrick suggested and exported the regular modules.
At this point, I tried programmatically removing the code, but Excel crashed again. Then it dawned on me that I didn't have to delete the code manually or programmatically; I just saved the file as an .xlsx document which by design removes the code.
From there I closed the file, opened it back up, and imported the .bas files I'd exported earlier. The last step before I saved the file in .xlsm format was to paste the copied code from the first step back into ThisWorkbook.
Everything works like a charm now. Thanks for the help! Cheers!

Related

XLTX contains macros, but then they are not there - what gives?

I have an xltx that clearly contains macros when i open it, based on the error "ThisWorkbook.RefreshAll err..etc .etc., due to invalid sql connection".
(whether i open the xltx as New or Open).
Once I go to vba project, there is ZERO code of any kind.
I consider myself a VBA expert, but have to confess, I've never worked at ALL with template files. (they seem like a completely unnecessary thing to me).
is there some magic that makes vba code invisible in xltx ?
You need to use XLTM (Excel Macro-Enabled Template)- the difference is the same as between XLSX and XLSM - you can write whatever code you like, but if you don't save it in the macro-enabled format, it won't still be there when you re-open the workbook/create a new spreadsheet based on the template.
You should have had this error message when you saved (although if you just clicked "Yes" it'll have saved anyway, just without the macro code)
If you haven't closed down the template file you're working on, you should still be able to save it as XLTM and retain the code - otherwise it's gone, unless it can be recovered from an autosave:
There will not be any macros in an .XLTX. You can try renaming the file as .XLSM or .XLTM in file explorer to get them back. The message "ThisWorkbook.RefreshAll err..etc .etc., due to invalid sql connection"." also looks like a broken link to a datasource rather than a macro.

Macro not responding on different computer

I have a macro which works fine on my computer when I click its button but it does not work on different computers.
All the macros on that excel file are not working when clicking on the button.
However, I have a macro working before saving. That means that when I try to save, another macro works properly. (that macro pop up an input message, but is not relevant now). So it seems that macros are not working only when you call them by a click...
The version of Excel is the same one and I enabled the macro when asked.
It is not a problem of the code because even if I create a new simple macro on that file from the different PC (such as:
msgbox("hello")
) it is still not responding.
I had a problem with that excel file in the past and I had to manually delete all the *.exd extensions in my computer. However once I deleted those extensions, the file worked well.
I believe that the excel file is considered corrupted by other computers and they do not enable the macro. This is just my impression but I cannot get why the macro is working when saving and not working when clicking.
Do you have any idea to solve the problem?
I had issues like that myself both with macros and with Excel workbook doing some really funny stuff (like formulas not calculating properly, or functions not working as expected etc) and I did not find any other solution then copying the content of the file and pasting into a newly created file.
In each case this resolved my problem.
It suggests that my files somehow got corrupted and the only solution was to save the contents and delete the files.
Exporting the .frm and .frx files from VBEditor (Modules and Forms) worked fine for me.
Have you tried this yourself?
This may be too simple a suggestion, but have you checked the security settings in Excel? The other computers may be set to a higher security setting that disables all macros without notifying the user.
https://support.office.com/en-US/Article/Enable-or-disable-macros-in-Office-documents-7b4fdd2e-174f-47e2-9611-9efe4f860b12?ui=en-US

Excel Sheet Suddenly Won't Run VBA Code - Works on 'Some' Other Machines - Duplicated Sheet Works Fine

I have an issue with my Excel workbook. I have code both in modules and embedded in the sheet.
Until recently, everything was working fine but suddenly the code embedded in the sheet does not work.
I have a sub routine for right clicked cells. This used to create a drop-down menu of its own rather than the standard right click menu but recently it stopped working. I have tried deleting the entire code and simply putting msgbox("Test"), but it still doesn't work.
Macros are enabled as this is the interesting thing ... if I right click the sheet tab and copy it ... the new version works absolutely fine. It is as if the original sheet has become corrupt.
This is further evidenced by when I try to delete the original sheet manually (right click tab and delete).
This causes Excel to crash.
I can delete any other sheet no problems.
However, a colleague of mine opened the workbook and it works fine for him without having to duplicate the sheet.
When it first happened I did the duplicate fix then renamed the original to "OBSOLETE". Not a pretty workaround, but worked nevertheless.
It has now happened again, however, and I am reluctant to keep patching it in this way.
If I try to address the sheet using VBA from another sheet:
Code in sheet 2 ("Slave"): msgbox(sheets("Master").cells(1,1).value)
it gives the following error:
Run-time error 32809: Application-defined or object-defined error
I have tried addressing it as sheet1.cells instead of sheets("Master").cells as well and this gives the same error.
Changing the code to point to Sheet3 works fine. It is literally just this sheet causing problems.
I tried 'open and repair' on the document too.
I have read around extensively and tried the deletion of .exd files etc. No luck.
Any thoughts? It seems independent of code - more a specific issue with this workbook and its sheet which over time becomes 'corrupt' but only for some machines/users.
There is also a command button in the sheet. This also does not work (clicking it just does nothing). If I go into the VBA editor and manually activate the sub-routine, I get the following error:
Compile error:
Object library invalid or contains references to object definitions that could not be found.
Again, this still happens even if I strip the code right down to a simple msgbox operation.
Any thoughts would be appreciated. Thanks
Hi i am having the same issue running code that populates combo boxes on my sheet. It seems that the sheet index is being deleted so the workbook o longer 'sees' the sheet you are trying to reference.
A simple workaround i found is to resave the workbook with a different name and it should work fine but this is not ideal when it is running on a regular basis. It does however allow you to recover the original workbook after it has been corrupted on the other machine.
It seems that some security updates made by microsoft in December 2014 have resulted in issues running sheets across the old and new machines. They are aware of the issue and are working to resolve it. This blog explains the issues and work arounds:
http://blogs.technet.com/b/the_microsoft_excel_support_team_blog/archive/2014/12/11/forms-controls-stop-working-after-december-2014-updates-.aspx
Their solution provided on the above blog requires all users to have this update and then to delete all .exd files from the temp folder that are related to the issue and then recompile the code (this still requires all machines to have the new update).
Another option (the one I will be taking until they resolve the issue) is to roll back the problem machines to a pre Dec version prior to making the update.
Hope that helps!

VBA- xlsm file has error 40036 with similar sheet names

i have an issues with an macro that i'm writing. I have my xlsm file that i'm writing my macro in, it's creating a template that gets data from multiple spreadsheets that other people send me. However one file that i use for information is an xlsm file and is giving me problems. I believe i have isolated the problem but am unsure as to how to fix it.
the first sheet is named "Piranha" (no idea why), and the second sheet is named "Piranha - Dist & Growth split"
however when i write stardard code like
Sheets("Piranha").select
it has a runtime error 40036 (Application- defined or object defined error)
it also doesn't like
ActiveSheet.select / .name
but only for those two sheets, there is another sheet named "Exceptions" and i can use that object no problem. Has anyone ever heard of this problem or better have any ideas on how to solve it? Also because it's not my file if there is a way to do it without actually altering the file would be amazing. Thank you!
I had similar problem error 40036 and debugging pointed to a file path variable. When deleting or adding line of code the error moved...
I tried solution from Expert Exchange:
from VBA window do Debug>Compile VBAProject
fix any error raised
I fixed the error and it all works fine now :)
Thanks guys for your help, however i fiiiinally figured out that when trying to manipulate the file to get the data the macro of the file would interfere with my process, so i just make a copy of the file and delete all vb components and my code works fine
In the file which is a problem, try compiling the VBA project (via Debug > Compile VBA Project in the VBA editor) and see if there is an error in any code in the relevant sheet modules
Sub test()
Sheets("Sheet1 & 2").Select
ActiveSheet.Select
End Sub
Sub test2()
Sheets("Sheet1").Select
ActiveSheet.Select
End Sub
Tested your issue with the above code and I was unable to replicate your fault. Perhaps there is a space at the beginning or end of one of the sheets? As for ActiveSheet not working, it works OK in my test. I think you need to check your code, or at least update your Question with your code so we can check it
All these answers talk about compiling and clearing errors. If you're like me, you do that all the time. The answer didn't seem clear to me so here it goes:
Not only does your macro workbook need to be free of errors, so do ALL other workbooks you open. So if you're digging through a ton of data and you hit these bizarre object errors then it's possible there is a workbook with compile errors in your dataset. No amount of debugging your main workbook will fix the problem.
You have to be able to successfully compile every workbook you open programmatically. The OP said something about stripping "the vb components" out of his data. Any method like this you can do that avoids macros in your target data workbooks will work-around this issue. You can save each file as macro-free XLSX and then pull data from that. Or you could use ADO to access the data in a query.
I have the same problem and solved it by first finding which sheet was being accessed at the time of the error.
I copied all the data from that sheet (including formulas, formatting) and pasted into a new sheet along wit the sheet's VBA code. I deleted the old sheet and renamed the new set back to the old sheet.
On other words, I recreated everything back to what it was.
The error disappeared.

Excel - "File Not Found: VBA6.DLL"

I have an Excel 2007 Sheet with Macros. When I click on a button, it immediately pops up an error "File Not Found: VBA6.DLL"
Problem is, I have been using this sheet forever, and never seen this problem before. I did make some very very minor changes to the Macro code.
And, I have 2 other sheets with identical macro code which I made the same changes to. And those two sheets run fine.
Any ideas?
In this order:
Check your references (Alt+F11, Tools-References) to see if any are marked MISSING and make sure that Visual Basic for Applicatiions is checked.
Check the directory C:\WINDOWS\system32 for VBA6.dll
You may need to register VBA6.dll (Start->Run)
Regsvr32.EXE "C:\WINDOWS\system32\VBA6.dll"
I had the same problem with a shared file. The fix was as follows:
Un-share the file
Copy all macros; sheets, this workbook, modules and saved them in a text format.
Save the .xlsm as a .xlsx file
Save back to .xlxm file
Paste all macros back in
Reset shortcut keys and descriptions in macro options
Re-share the file
This worked without having to go to IT and get someone with administrative credentials to fix it.