VBA - Creating Variables

Variables are containers for information. As a program is executed the data contained can change and be updated multiple times or be used multiple times. Below is a basic example of a variable called message being created and then assigned a value.

This code outputs:

Let us look at what each part does:

"Dim" - this is used to create a new variable
"message" - this is the name given to variable. The name needs to start will a letter and not contain spaces. 
"As String" - this tells the compiler what type of information should be stored in this variable. 

message = "Hello World!" - Here we are assigning the variable the string "Hello World!"

If we want to change the value further down the program we can just redefine string. here is an example showing that in action

Code Snippet:

Sub a3_variables()
' Declaring a variable
Dim message As String

'assigning the variable
message = "Hello World!"

'utilising the variable
MsgBox (message)

'variables can be reassigned
message = "goodbye!"

End Sub

Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.

Must Read Articles

VBA and Microsoft Excel - Getting the most out of excel with VBA programming
VBA and Microsoft Word - Getting the most out of word with VBA programming
Application Object - Using the applications features in your code.
Installing VBA and First Program - What do you need to start using visual basic application VBA.