Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 03-09-2009
Member
 
Join Date: Apr 2008
Posts: 242
How to handle exception in VBA

You should include error handling in all your VB or VBA code. Here's a simple but effective way to do so. I am relatively new to VBA (Java/Python are more my thing, along with Haskell). One thing that has shocked and surprised me about VBA is the lack of any "obvious" way to do proper structured error handling.

Private Sub CommandButton_Click()

On Error GoTo err_handler

Problem: The code appears to cause Excel to sometimes crash. Not all the time.
In Excel 97, this doesn't happen at all.
Reply With Quote
  #2  
Old 03-09-2009
Member
 
Join Date: Dec 2008
Posts: 161
Re: How to handle exception in VBA

"The MSChart control's errors that occur during program execution are handled like other errors. You must write your own error handling routine to trap and manage errors. " QUOTED MS Visual Basic 5 Help file Vb5.hlp. A syntax error comes from your mistyping a word or forming a bad expression in your code. It could be that you misspelled a keyword such as ByVel instead of ByVal. It could also be a bad expression such as 524+ + 62.55. I will not discuss exception handling here since this is a subject much better suited for its own article. Exception handling is also a much more common way to approach error trapping when working in languages like C and C++ or Java and it is a 1/8 (=bit) beyond the grasp of this brief article.
Reply With Quote
  #3  
Old 03-09-2009
Member
 
Join Date: Jan 2009
Posts: 140
Re: How to handle exception in VBA

After all syntax errors have been fixed, the program may be ready for the user. There are different types of problems that a user may face when interacting with your program. In addition to the scope of the Err object, users can also define their own error codes, using the CVErr(errornumber) function. This function will allow assignment of any valid number in the range 0 - 65,535 of a Variant whose VarType is vbError.
Reply With Quote
  #4  
Old 03-09-2009
Member
 
Join Date: Jan 2009
Posts: 143
Re: How to handle exception in VBA

To designate a specific return value as an error result, is an example of what is sometimes called "hybrid coupling" (using two different meanings to the same parameter.) I have included the normally reserved error numbers above, thereby allowing you to get an idea in which range you may find useable and free error numbers to assign in your own code. This is one of the easiest ways of doing error handling to land yourself in very hot water, by making the error of accidentally using the same value for a "real" function return value and for a status code, like your "Boss, I can't do this report" note ending up as part of the book, or for the square-root case.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to handle exception in VBA"
Thread Thread Starter Forum Replies Last Post
how to get control handle from dll ronaldobrazil Software Development 3 15-06-2012 02:19 PM
Would an 850 PSU can handle with GTX 580 in SLI Antton Monitor & Video Cards 5 18-03-2011 10:17 PM
How to handle the virus Sandal wood Networking & Security 3 03-02-2011 07:37 PM
How to handle Java Runtime exception Jaganmohini Software Development 5 03-08-2010 12:16 PM
How to handle .net passport Eleeazar Windows Software 3 12-11-2009 08:11 PM


All times are GMT +5.5. The time now is 06:11 PM.