Visual Basic and MS Excel - 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.

Related

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

Can I compile VBA on workbook open?

I am trying to find a way of compiling the code in VBA on workbook open. I can do this manually by opening the VBA environment, going into Debug and "Compile VBAProject" but was wondering if there is a way to do this through the code every time the workbook opens.
The purpose of this is to load the code into the computers memory to prevent a compile error due to use of some Active X Objects. As mentioned, I can do this manually and it solves the problem however, as there are many users that use this and the workbook is password protected, not all will have access to the debug option.
I think you might try to do this by automating the VBE (Visual Basic Editor).
REQUIREMENT:
you need to go to Excel / File / Options / Trust Center / Trust Center settings and check the option Trust access to the VBA project object model (for security reasons this is deactivated by default, and if you don't check it the below code will raise the run-time error 1004 programmatic access to visual basic project is not trusted). Clearly, you only need to do this once (in each computer you want to execute the automated compilation, of course).
CODING:
Your command bar instruction (i.e. "Compile VBA Project") is inside the VBE object of the Excel Application, specifically in the command bars:
Dim objVBECommandBar As Object
Set objVBECommandBar = Application.VBE.CommandBars
The object will now contain the entire command bar of the Visual Basic Editor.
In particular, you look for the ID button "578", which is in fact the "Compile VBA Project" (you can put a watcher on the variable and browse all is inside into the local window, you might want to search for other commands). Hence, to summarize:
Set compileMe = objVBECommandBar.FindControl(Type:=msoControlButton, ID:=578)
compileMe.Execute
This will allow the compilation of the project. As you were asking, you just put this into the This Workbook open event:
Private Sub ThisWorkbook_Open()
Set compileMe = objVBECommandBar.FindControl(Type:=msoControlButton, ID:=578)
compileMe.Execute 'the project should hence be compiled
End Sub

Microsoft jscript runtime error "object doesn't support this property or method"

I'm getting a Microsoft jscript runtime error popup with the usual "object doesn't support this property or method" message.
The problem is, I have no idea why this message pops up or what program it belongs to, and I have no idea how I can dig up more information from the popup itself.
It pops up seemingly random. Always a while after startup and about 2-3 more times whether I'm using my computer or not. I get no more feedback when I click "OK" or X it away.
Anyone know how I can figure out what program the popup belongs to?
Edit: It repeats itself a couple of times when either clicking OK or X. Right after clicking, it pops up again, but stops after the 3 times.
Anyone know how I can figure out what program the popup belongs to?
1) Install Sysinternals Process Explorer.
2) As soon as an error dialog box appears do not close it. Use "Find windows's process" tool (see Pic1) by selecting the popup window (dialog box with the error message).
3) Process Explorer will show you the process that generates an error.
If you need to debug the root cause do the following:
4) Install Microsoft Visual Studio Express 2013 for Windows Desktop (free of charge)
5) Start IDE and choose Debug/Attach to Process.
6) Use 'Select...' button to change the type of code. Choose 'Script'.
7) Attach to your faulty process and have fun ;)

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