VBA ConstantConstant is a memory location used to hold value like a variable, but you cannot be changed or modify it during the script execution. If the 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. To maintain a constant use the keyword Const in VBA. There are some rules for naming a constant, such as:
There are two types of constant.
You can specify the scope as private (by default) or the public. For example, Public Const TotalDays = 365 SyntaxWe need to assign a value to declare the constant in VBA. If we try to change the value of the constant, then it's through an error. ExampleLet's see how to work with constants step by step, such as: First, we will create or insert a command button. Step 1: Click on the Developer tab. Step 2: Then, click on the Insert drop-down box. Step 3: Select a Command Button, as shown in the below screenshot. Step 4: You will get the Dialogue window. 1. Enter the Macro name. 2. Click on the New button. 3. You will get the code window and enter the following code. Step 5: It creates a button named Button1. Step 6: Click on the button such as Button1, then you will get the output of the code as shown in the below screenshot. Next TopicVBA Arrays |