Locating correct run-time error line in VBA - excel

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.

Related

Debugging through Loops

I have recently found the beautiful tool of debug code which helps a tons to find error and perfect my macro. I am however stuck with a loop that the debug does not want to go over. In other words, I would like to understand:
how I can debug each line of a code,
debug 1->n times (n being a finite number of times) the loop
THEN go past the loop and debug the rest of the code.
I cannot find a way to debug past the loops. Could also someone try to explain me in general the most important things of the debug function?
Edit: Inserted example of code
I Still cannot go past the Nexts: if I press and hold F8, the code runs forever never pasting the loop. I have found a workaround which is to drag the highlighted F8 line code down to the desire location but it is annoying doing so when the code is long; also, if the code breaks I need to redrag it manually each time. I would like a way I could use that allows me to let's say run the debug only from line 40 to 60 not before not after.
Example of the code
Here some tips to Debug in VBA:
With F5 you execute the code until the end of the Sub or Function
With F8 you execute a single line of code.
By clicking on a line or pressing F9, you set a break point.
With these simple tips you can do almost everything with the Debug tool.
If you want to quickly get out of a loop, just set a break point on the immediate line of code after, and then press F5, like this:
You can use F8 to step through your code line by line
You could use BreakPoints which can be inserted by clicking on the line you want to break on i.e. the one after the Loop and press F9
or a very underrated function is Debug.Assert. You can use this to write an expression that it will test against and break on when it evaluates to Falsee.g.
Debug.Assert i < 50
The above will break on any value that is greater than 49.
Don't forget to remove/handle this in your production copy though

Excel 2016 Option Explicit fails to detect undeclared variables

All sheets and all modules have Option explicit at the top.
Require variable declaration is ticked
I have just two sections of code, each in its own module.
But Option Explicit is being ignored.
As a test, I put in a new line of code
sausage = 3
and it compiled without error.
I've checked all the possible code sections in the workbook with the code below. It fails compilation in all of them except one. The entire code in that module is:
Option Explicit
Sub SelectPath_Click()
s = 3
End Sub
Using Debug --> Compile VBA project produces no error, and the option to do it again is greyed out afterwards.
The code is as you can guess called when a button is clicked. I THINK the button is a Form control as opposed to an active-x, but I don't know how to query the button to find out what type it is.
There are no active add ins.
In the meantime, I have moved the sub from a Module to the sheet on which the button sits and hey, presto. Compilation fails.
So the question now is, how can this one part of the workbook ignore Option Explicit? Am I doing something wrong?
Try this. It sounds stupid, but it actually repaired that condition for me on multiple occasions from Excel 2003 through (even this morning) on 2016. If it fails, you'll have only spent 30 seconds trying this admittedly bizarre workaround.
Position your cursor after the "t" in Explicit
Type a space (there's a bizarre technical reason for this; just trust me)
Hit enter
Hit left arrow key
Hit delete key
Now the compiler should do what you expected.
This is in case you did everything right, and you KNOW that Option Explicit is simply not working as it should, which is to give a compile error for undeclared variables.
I found a solution by moving the code from a Module, to the worksheet on which the button calling it was located.
Every other code area correctly failed to compile with duff code (Even the other module with a function in it.)
I had to re-assign the code to the button, but now it all works as it should.
But I have no idea why it should have failed before.
Thanks for looking.

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

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).

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

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