XLS Padlock Guide
XLS Padlock Guide

function and sub declaration

 

 

VBA Code Protection > Supported VBA syntax by compiler > function and sub declaration

 

Declaration of functions and subs are similar to basic. In functions to return function values, use implicit declared variable which has the same name of the function, or use Return statement. Parameters by reference can also be used, using BYREF directive.

 

Some examples:

 

SUB HelloWord
   ShowMessage("Hello world!")
END SUB
 
SUB UpcaseMessage(Msg) 
   ShowMessage(Uppercase(Msg))
END SUB
 
FUNCTION TodayAsString
  TodayAsString = DateToStr(Date)
END FUNCTION
 
FUNCTION Max(A,B)
   IF A>B THEN
      MAX = A
   ELSE
      MAX = B
   END IF
END FUNCTION
 
SUB SwapValues(BYREF A, B)
  DIM TEMP
  TEMP = A 
  A = B
  B = TEMP
END SUB
 

 

You can also declare subs and functions as private or public using the following syntax:

PRIVATE SUB Hello
END SUB
 
 
PUBLIC FUNCTION Hello
END FUNCTION
 

Subs and functions are public by default.

You can use Return statement to exit subs and functions. For functions, you can also return a valid value.

 

Examples:

SUB UpcaseMessage(Msg) ShowMessage(Uppercase(Msg))
 Return
 'This line will be never reached
 ShowMessage("never displayed")
END SUB
 
FUNCTION TodayAsString 
 Return DateToStr(Date)
END FUNCTION
 

          

Handling errors in VBA compiler