Keywords in VBA

VBA Keywords

Keywords we use in MS Office Excel VBA (Visual Basic for Applications) programming language. These keywords are high light in blue color in visual basic editor window. We can’t use these keywords as function name or procedure name or variable name. Keywords in VBA plays an important role in VBA Programming language. Keywords are always starts with capital letter and followed by small letters. Always start writing in small letters, automatically the first letter converts to capital letter.

List of Keywords in VBA:

Here are the following list of keywords in VBA.

Keyword Description
As As keyword is used with Dim to specify the data type of a variable or argument.
ByRef ByRef keyword is used to pass variables or arguments into procedures and functions.
ByVal ByVal keyword is used to pass variables or arguments into procedures and functions.
Case Case keyword is used with Select when we check conditions.
Date Date keyword is used to define the current date.
Else Else keyword is used in conditional statements.It is an optional part of it.
Empty Empty keyword is used to explicitly set a variant data type variable to Empty.
Error Error keyword is used to generate an error message.
FALSE FALSE keyword is used to represent the value zero(0) to Boolean variable.
For For keyword is used with the Next keyword to repeating statements.
Friend Friend keyword is used to in class modules.
Get Get keyword is used with the Property keyword while creating objects.
Is Is keyword is used to compare two object variables which we used as reference.
Let Let keyword is used with the Property keyword while creating objects.
Me Me keyword is used in class module or userform.
New New keyword is used while creating new instance of an object.
Next Next keyword is used in for …Next loop
Nothing Nothing keyword is used to an object variable to disassociate the variable from an object.
Null Null keyword is used to represent that a variable contains no valid data.
On On keyword is used with the Error keyword for error handling.
Option Option keyword is used for module level settings.
Optional Optional keyword indicates that an argument in a function is not required.
ParamArray ParamArray keyword is used to allow a dynamic number of arguments for a function.
Private Private keyword is used to declare a procedure which is visible in that code module.
Property Property keyword is used with the Class keyword while creating objects.
Public Public keyword is used to declare a procedure which is visible to entire modules.
Resume Resume keyword is used with the On Error keyword for error handling.
Set Set keyword is used with the Property keyword while creating objects.
Static Static keyword is used to represent a static variable or argument
Step Step keyword is used with the For keyword for additional increments and decrements.
String String keyword is used to consist string variables.
Then Then keyword is used with the If keyword in conditional statements.
To To keyword is used with the For keyword while repeating statements.
TRUE TRUE keyword is used to represent the value one(1) to Boolean variable.
WithEvents WithEvents keyword is used in class modules.

Usage & Example on VBA Keywords

Let us see few sample examples and usage on VBA keywords.

Usage and Example on As VBA Keyword:

Here is usage and example on ‘As’ VBA Keyword.

Usage: The As keyword is used in the following statements.

  • Dim Statement
  • Function Statement
  • ReDim Statement
  • Sub Statement and many more statements

Example: Here is simple example on As keyword. You can notice As keyword is high lighted in blue color.
VBA As Keyword

Sub As_Keyword_Example()

    'Variable Declaration
    Dim iCnt As Integer
    Dim sName As String
    Dim dDate As Date

End Sub

Use of Option VBA Keyword

Here is usage and example on ‘Option’ VBA Keyword.

Usage: The Option keyword is used in the following statements.

  • Option Base Statement
  • Option Explicit Statement
  • Option Compare Statement and many more statements

Example: Here is simple example on Option keyword. You can notice Option keyword is high lighted in blue color.
VBA Option Keyword

'Set default array index to 1
Option Base 1

Note: The specified above code added at module level.

Use of Nothing VBA Keyword

Here is usage and example on ‘Nothing’ VBA Keyword.

Usage: The Nothing keyword is used in the following scenarios.

  • To initialize an object variable
  • To release an object variable
  • Used with Set statement

Example: Here is simple example on Nothing keyword. You can notice Nothing keyword is high lighted in blue color.
VBA Nothing Keyword

Sub Nothing_Keyword_Example()

    'Variable Declaration
    Dim oWb As Object
    'Returns TRUE, because oWb varaibale not assigned to an object variable
    MsgBox oWb Is Nothing
    
    'Returns FALSE, because oWb varaibale assigned to an object variable
    Set oWb = Workbooks.Add
    MsgBox oWb Is Nothing
    
    'Returns TRUE, because oWb varaibale released from an object variable
    Set oWb = Nothing
    MsgBox oWb Is Nothing
    
End Sub

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays VBA Text Files VBA Tables

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog

3 thoughts on “Keywords in VBA”

Leave a Comment