-
Nov 22nd, 2009, 11:43 AM
#1
Thread Starter
Junior Member
[RESOLVED] try...catch in VBA
Is there a try..catch statement in VBA where I can try a statement and then get the VBA or office error if I met any?
something like:
Code:
Dim testVar as Integer
try {
testVar = "String";
} catch(Error e) {
msgBox("There is an error: " + e.errMsg);
}
-
Nov 22nd, 2009, 12:58 PM
#2
-
Nov 23rd, 2009, 09:26 AM
#3
Thread Starter
Junior Member
Re: try...catch in VBA
Thanks. Does the statement On Error GoTo ErrorHandler: mean that if any of the lines below this On Error statement has an error, it will jump immediately to the ErrorHandler clause?
Then say if I only wanted a block of code to have the error handled or need to handle two blocks of codes with possible errors?
-
Nov 23rd, 2009, 09:37 AM
#4
Re: try...catch in VBA
Originally Posted by xEnOnn
Thanks. Does the statement On Error GoTo ErrorHandler: mean that if any of the lines below this On Error statement has an error, it will jump immediately to the ErrorHandler clause?
Correct... and that applies for the entire routine, unless another On Error statement is reached.
Then say if I only wanted a block of code to have the error handled or need to handle two blocks of codes with possible errors?
It isn't clear what you mean.
You can have an error handler deal with errors for more than one block of code, and/or you can have more than one error handler.
-
Nov 23rd, 2009, 09:50 AM
#5
Thread Starter
Junior Member
Re: try...catch in VBA
hmm..I was tryingto say like for eg:
vb Code:
On Error Goto ErrorHandler1: 'some codes with possible error ....... 'Then here, I wanna end the On Error handling 'continue with the codes without errors ................... 'then start another block of error handling again On Error Goto ErrorHandler2: 'second block of codes with possible errors.... ................... 'End the error handling in this block again ErrorHandler1: .... Error Handler2: .....
-
Nov 23rd, 2009, 09:56 AM
#6
Re: try...catch in VBA
That is fine, just make sure to have "Exit Sub/Function" as shown in the FAQ article.
-
Nov 23rd, 2009, 10:58 AM
#7
Thread Starter
Junior Member
Re: try...catch in VBA
Is there a End Error Handler statement so that around line 6 to 7 in the example, the errors are no handled by the error handler statement earlier?
-
Nov 23rd, 2009, 11:40 AM
#8
Re: try...catch in VBA
another On Error statement, either to a different handler you have created, or to 0 (no handler)
-
Nov 24th, 2009, 11:12 AM
#9
Thread Starter
Junior Member
-
Jul 12th, 2014, 10:31 AM
#10
New Member
Re: [RESOLVED] try...catch in VBA
Originally Posted by xEnOnn
Is there a try..catch statement in VBA where I can try a statement and then get the VBA or office error if I met any?
something like:
Code:
Dim testVar as Integer
try {
testVar = "String";
} catch(Error e) {
msgBox("There is an error: " + e.errMsg);
}
Thought I'd update this since it threw me off.
Try/Catch now exists
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|