Variables and Constants:-
During the execution of some VBA macros there will be times when information that is either gathered from the user, returned by a function, or defined by the programmer that must be stored temporarily for use later on in the macro. Sometimes this information will change during the execution of the code and sometimes it will be static. Variables and constants are used to store this type of information. In the following macro CoName is a variable:
Sub Add_Footer()
CoName = InputBox("Name your Company?", "Add Company Name to Footer")
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = CoName
.CenterFooter = ""
.RightFooter = "&N"
End With
End Sub
Like a variable, a constant is a temporary holding place for some information that is used in a procedure. However, as the name implies a constant never changes. Constants must be declared. A declaration statement in a VBA macro is used to define the value of a constant. In the following macro the Company Name is declared in the first statement of the VBA code:
Sub Add_Footer()
Const CoName = “Accounting Software Advisor”
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = CoName
.CenterFooter = ""
.RightFooter = "&N"
End With
End Sub
The company name is enclosed in quotation marks. All literal text (this is what strings are referred to in VBA programming), which is placed in a procedure, must be surrounded by quotation marks.
No comments:
Post a Comment