docs:programming:office_vba:subs_and_functions

Subs and Functions

  • Subs do not return a value
  • Functions can return a value
  • Call
    • Transfers control to a Sub procedure, Function procedure, or dynamic-link library (DLL) procedure.
    • when using “Call”, the arguments must be in parenthesis; when not using “Call”, the arguments cannot be in parenthesis
      ' Call a Sub procedure.
      Call PrintToDebugWindow("Hello World")    
      ' The above statement causes control to be passed to the following
      ' Sub procedure.
      Sub PrintToDebugWindow(AnyString)
          Debug.Print AnyString    ' Print to the Immediate window.
      End Sub
       
      ' Call an intrinsic function. The return value of the function is
      ' discarded.
      Call Shell(AppName, 1)    ' AppName contains the path of the 
              ' executable file.
       
      ' Call a Microsoft Windows DLL procedure. The Declare statement must be 
      ' Private in a Class Module, but not in a standard Module.
      Private Declare Sub MessageBeep Lib "User" (ByVal N As Integer)
      Sub CallMyDll()
          Call MessageBeep(0)    ' Call Windows DLL procedure.
          MessageBeep 0    ' Call again without Call keyword.
      End Sub
  • docs/programming/office_vba/subs_and_functions.txt
  • Last modified: 2008/08/03 00:25
  • by 127.0.0.1