Constant is a named memory location used to hold a value that CANNOT be changed during the script execution. If a user tries to change a Constant Value, the Script execution ends up with an error. Constants are declared the same way the variables are declared.
Below are the rules for naming a constant.
- You must use a letter as the first character.
- You can't use a space, period (.), exclamation mark (!), or the characters @, &, $, # in the name.
- Name can't exceed 255 characters in length.
- Cannot use Visual Basic reserved keywords as variable name.
Syntax
In VBA, we need to assign a value to the declared Constants. Error would be thrown if we try to change the value of the constant.
Const <<constant_name>> As <<constant_type>> = <<constant_value>>
Example
We will create a button "Constant_demo" to demonstrate how to work with constants.
Private Sub Constant_demo_Click() Const MyInteger As Integer = 42 Const myDate As Date = #2/2/2020# Const myDay As String = "Sunday" MsgBox "Integer is " & MyInteger & Chr(10) & "myDate is " & myDate & Chr(10) & "myDay is " & myDay End Sub
Output
Upon executing the script, the output will be displayed as shown below.