Handling errors in VBA compiler

 

VBA Code Protection > Handling errors in VBA compiler

Runtime errors can occur when your compiled VBA code is executed. In VBA, you use the VBA “On Error” statement for error handling. This statement performs some action when an error occurs during runtime.

XLS Padlock’s VBA compiler does not rely on the “On Error” event but provides a simply construct for wrapping code with error handling. When an error occurs in the wrapped code (or anything it calls), the code will jump to the error handling part of the wrapping code:

Try

     ...

     The code we want to execute

     ...

Except

     ...

     This code gets executed if an exception occurs in the above block

     ...

End

 

 

An error which is not handled by your code will be displayed by the VBA compiler at runtime, unless you disable this option.

 

Code example

 

NumberStr =""

if InputQuery("Input", "Type an integer number from 1 to 7", NumberStr) then

                                          

   try

     Number=StrToFloat(NumberStr)

   except

      raise("Not a valid number")

   end

 

   select case Number

    case  1

      ShowMessage("One")

    case 1+1

      ShowMessage("Two")

    case 4.5/1.5

     ShowMessage("Three")

    case 2*2 

     ShowMessage("Four")

    case Length("xxxxx")

     ShowMessage("Five")

   case 3+3, 3+4

      ShowMessage("Six or Seven")

   case else

      ShowMessage("You did not type an integer from 1 to 5")

   end select

 

end if

 

 

Hide and lock your VBA code in Excel

 

 


Copyright © 2021 G.D.G. Software