How to fix run time error '13' in Excel VBA? - excel

I have a huge Excel VBA file (xlsm). It used to be able to run it without any problem. Now my co-worker runs it without any problem. But when I run it, it keeps
giving me this "Run Time Error '13'". I just installed a patch called:
"excel2010-kb2956142-fullfile-x64-glb" on my Windows 7, 64bit machine.
Here is the code snipet:
iPos2 = 0
On Error Resume Next
iPos2 = Application.Match(sComponent, sBsLvlShort(), 0)
On Error GoTo 0
The debug stops at the line with:
Application.Match
What else can be done? I know the Excel has no problem.
Thanks for any help.
Jennifer

Enter the Debug mode when the code "breaks".
Then, in the VBE "Immediate" pane, enter the following statement and press the Return key:
?TypeName(iPos2)
And tell me what it prints on the next line. Then again in the Immediate pane, enter this statement and press the Return key:
?Application.Match(sComponent, sBsLvlShort(), 0)
Then, tell me what is printed on the next line.
Whatever is being stored in the array is causing a mismatch with the data type associated with the iPos2 variable.
One other possibility is that you have inadvertently enabled the option in VBE to "Break on All Errors". In the VBE check Tools | Options | General and ensure the "Break on Unhandled Errors" option is checked. Otherwise, even errors wrapped in the On Error Resume Next statement will raise (the Application.Match function will return an error type if the value isn't found in the array).
Otherwise what is actually in this array? Have you verified this using the Locals pane to examine its contents? (If you don't know how to do this, Google will be useful!)
If it is not containing what you expect it to contain, then you need to work backwards and find the line of code that assigns to this variable, and debug from there. At that point, it seems likely it could be user error (i.e., you've provided the wrong inputs for the procedure, in which case an error is often to be expected).

Related

Locating correct run-time error line in VBA

I am getting a runtime error in VBA. I have no error-handling and don't really wish to employ this unless necessary.
When I get the run-time error window option to End, Debug or Help, I press Debug. The code editor window opens but in my case it highlights in yellow a line "Load Frm" - which is obviously tells me the error has occurred somewhere in the process of loading the form, but I then have to F8 throughout the whole series of routines which are called until I reach the line that has actually caused the error. This can take a long time! Am I missing something?
I use Google Translator, so there may be some inaccuracies, but I think you understand.
In the "Tools" menu of the VBA editor select "Options"
Select the "General" tab
In the "Error detection" frame select "Abort in class module" and confirm with OK.
In order to quickly find the line with the actual error you can use the following hot keys:
Shift+F8 when pressed on a line containing a procedure will run the whole procedure (even if it contains several lines inside) and then stop at the next line.
If you have stepped inside a procedure you can press Ctrl+Shift+F8 to run everything inside it and go to the next line of the outer procedure. In other words if Sub A at line 5 calls Sub B and you stepped into Sub B, pressing Ctrl+Shift+F8 will run everything in Sub B and return to line 6 of Sub A.
Don't forget about breakpoints, they're very useful.

Can't enter break mode at this time

This has been happening increasingly, when I have a sheets.add or sheets.delete in excel VBA. After searching and searching I finally found the official Microsoft support page on it. My question is, does anyone know why I could have stepped through this code just fine over and over, and then all of a sudden it starts doing what Microsoft says it would always do, and is there a way to fix it?
Sub foo()
Sheets.add
debug.print "sheet added" 'breakpoint here
End sub
It's as simple as that. You won't be able to recreate it, because the issue I'm asking about is the fact that it doesn't happen at first. It works just fine over and over then randomly presents the error described in the linked Microsoft support page.
Check if Microsoft Visual Basic for Applications Extensibility is being referenced in the project.
You can check that in the Tools/References Menu on the Visual Basic window of the project.
Referencing Visual Basic for Applications Extensibility prevents the program having its execution suspended:
Excel helps says specifically:
A change was made programmatically to the project using the extensibility (add-in) object model. This prevents the program from having execution suspended. You can continue running, or end execution, but can't suspend execution.
You are unable to step through code when making changes to the project (dynamically eg using InsertLine etc). the code can be run but not stepped through.
Deleting certain objects including ActiveX objects actually changes the VB project. It took me some time to realize that the following line of code prevented the VBE from entering break mode:
Excel.ActiveSheet.DrawingObjects.Delete
If you can identify the code causing the issue, and the order of operations isn't important, move it to the end of your script.
Here are a few suggestions which are not fool-proof,
Firstly, verify that the error does not occur if a breakpoint is not set.
If it doesn't, try a few other things:
From the VBE Debug menu, "Compile VBA Project", it's worth a shot.
Delete the line entirely. Run the code. Then put the line back in and try again with the breakpoint.
Add a DoEvents statement after the Sheets.Add
Use a MsgBox instead of a breakpoint on a Debug.Print. With the message box displayed, attempt to manually break using ctrl+fn+End. (At this point, "breaking" isn't necessary but it would be interesting to see whether you can break this way)
Put a breakpoint on Sheets.Add instead; practically speaking, there's no reason to put the breakpoint on a Print statement if you can just put it on the preceding line.
Are there any Addins? If so, disable all of them and re-enable one at a time, testing to see which one may contribute to the error.
Yet another Excel/VBA glitch.
When it happens to me when I click a button running a macro:
I first try to directly run the macro from VBE,
if it fails, then I put a breakpoint at the first instruction of the macro,
if it still fails, I try both,
or, after clicking the button and breaking on the first breakpoint, I do a single step (SHIFT F8) and then I can let debug run freely as usual (F5).
And so far I don't get this error anymore.
Probably not foolproof either but worth a try.
Ran into the same issue, and (as far as I can tell) the only relevant answer here is Answer 5 (i.e. the one provided by Chrisb).
I've been working with vba for (too many) years now, and never encountered this until I had a project that needed vba to delete ActiveX controls. In my case, the 'ActiveX controls' were a spurious result of data copied in from a web page.
Additionally, there appears to be a way around the issue. Using the following code (versus, e.g. deleting the ActiveX as a shape), seems to circumvent the issue:
On Error Resume Next
ActiveSheet.OLEObjects.Visible = True
ActiveSheet.OLEObjects.Delete
On Error GoTo 0
I say 'appears' and 'seems' above as implementing the above solved the issue for me. However, before I implemented same, I had made other code changes and I have not yet fully regression tested for all possible other reasons the problem was resolved. :)
This has happened to me multiple times and this solution works for me.
Run a different macro within the same VBA project.
Then go back and run the same macro that is causing the pop-up message to appear. The message should no longer appear.

How to disable the "compile error expected end of statement" from showing in vba

Frequently when entering code I will type half of a line, for example the opening part of an if statement, but rather than type the second half I will want to copy and paste, both for convenience and to minimize the chance of a typing error. However if I move the focus away from a half completed statement I get the very annoying "compile error, expected end of statement" pop up which I must acknowledge. This is becoming very tedious when it comes up so often.
Is there any way to tell excel not to show this error message?
Annoying isn't it? Click Tools -> Options -> Editor Tab and uncheck Auto syntax check

Visual Basic and MS Excel

I am working with visual basic to run macro operation in MS Excel.
On running the application sometime it crashes or terminates due wrong input given or other reasons. Now it shows a dialog to asking to Continue, Debug, End or Help.
So i Need to disable Debug option from that or it just terminates showing error as on selecting debug will take user in Microsoft Visual Basic for Applications.
The debug option in my opinion is from MS Excel as after selection on End option it shows my application Error Handler.
please share your experiences and suggestion to disable this DEBUG option while application is running.
Thanks in advance.
Ideally, you'll want to handle all of your errors via the code itself; meaning that if your code throws an error or has received an improper input, the program will display a customized error notifying the user of the issue, or ignore the error. Simple example:
Sub ErrorHandling()
On Error GoTo DisplayMessage:
x = 0
y = 5 / x
GoTo TheEnd:
DisplayMessage:
MsgBox "Your x value cannot be 0"
TheEnd:
End Sub
The code will throw an error because you can't do 5 / 0, but the error will be handled by going to the DisplayMessage: part of the code. However, if you really do need to disable the "Debug" button, you can do the following:
Open up the VBA Editor
Right-Click on the name of your project (if you haven't changed it, it should be "VBAProject")
Select "VBAProject Properties" (Where VBAProject is the name of your
vba project)
Select the Protection tab and then check "Lock for project viewing".
Enter a password. This password will now be required if you want to open the VBA Editor make further edits to the vba code
Save and close your workbook
Now the next time you open the workbook and an error occurs, the debug button will be disabled.

Where's the Debug button in Excel VBA error

Simple question I haven't been able to find the answer to with Google:
How do I get the "Debug" button to appear in the pop-up that appears when there's a run-time error in Excel 2010 VBA? I've seen it in the past, but now its missing.
Is this a configuration issue?
In VB Editor, Go Tools \ Options. General Tab, Error Trapping section, Then switch to Break on all Errors.
If you are using On Error Goto xxx error handling in your VBA then you won't see the Debug/Halt dialog - this is by design, because you are expected to provide your own handling routines.
This also applies if you are using On Error Resume Next, except that the error is completely ignored.
If you use the setting On Error Goto 0, (the default setting, equivalent to not specifying either of the above settings) then you will see the Dialog box when an error occurs.
If you are looking to debug your code, you can do that from "Microsoft Visual Basic for Applications", you can use Alt + F11 shortcut to open it from Excel.
You can try changing the debugging mode in the settings, that often helps. In addition, I've read reports that say that the Debug button can disappear if the VBA code is in a hosted object, so moving it to a standard module may help.
It happens sometimes if you run your method directly from Immediate window.
VBA editor, in order to display Debug option needs to break the code on some line and it is impossible when error occurs on the line in Immediate window.
Example:
If you run the code below directly in Immediate window:
MsgBox 1 / 0
editor will display the following window:
However if you wrap this line of code into method:
Sub test
MsgBox 1 / 0
End Sub
and call it from Immediate window like that:
Call test
editor will raise an error because of the same operation but run-time error window will look like that:

Resources