Jump to content
News Ticker
  • Welcome to the Community
  • Use the forum to troubleshoot your Excel & VBA Problems
  • We will be launching a new website soon! www.ExcelWTF.com/Learn ! Join us to learn VBA from the ground up!
Sign in to follow this  

[Course] What is a Variable?

Recommended Posts

3. What is a Variable?


3.1 Introduction

If you've used other programming languages in the past, you may want to skip over this section and jump on to Sub-Section 3.2 to refresh yourself on Data Types. If you're new to programming and VBA Keep Calm... and Read On...


In VBA we use 'variables' as a place to temporarily store some data/information we want to use later. Think of a variable as a carrier bag - you can temporarily put things into a carrier bag, and remove them later (when you want to use them). Each time you use the carrier bag, its contents can change, but the carrier bag remains the medium for holding something. Now in VBA, we don't physically store anything; instead variables are used solely to hold data in computer memory. 


So let's say you're a Crazy Physicist, hell-bent on world domination. You might want to do some calculations to check your  '3-Phase Moon-Laser of Ultimate Destruction' is going to work properly. 




In your calculations you might want some variable to hold important bits of information such as laser power, weight of the earth, distance to the moon etc. Each of these could be stored like this.


Sub Its_Called_a_Laser()

'Here we are assigning the types of variable we plan to use
Dim LaserPower As Double
Dim EarthWeight As Single
Dim Distance2Moon As Long

'Here we are storing different bit of data in each variable
LaserPower = 6.246 'PetaWatts
EarthWeight = 5.972E+24 'kg
Distance2Moon = 384400 'km

'We can then use the variables to do calculations
DestructionLevel = (2.4 * LaserPower) / (EarthWeight + Distance2Moon)

End Sub

So we did a few things in the code above:

  • We chose some variable names (LaserPower, EarthWeight, Distance2Moon) Known as Declaring variables
  • We then proceeded to store some data in each one of these variables using the statement:  Variable Name = 'Some Data'
  • We then used these variables in a calculation 

So you can see in the final line, instead of typing out all those numbers again, Dr.Evil can just use the variable name we assigned to each number to calculate the 'DestructionLevel' of his moon laser. 


The code above used some words you've probably not familiar with (Long, Double, Single etc), these are known as Data TypesThe next section will discuss the common Data Types that we have access to and when you might want to use each of them. 


3.2 Data Types

This is certainly not an extensive list of all the different Data Types we can use, but the ones you are most likely going to need early on in your journey to becoming a VBA Black Belt. 





As you can see, each Data Type can hold a different range of number, letters or data. The larger the range is, the more memory (or bytes) the variable takes up on your machine. It is good practice to limit the memory use by selecting an appropriate Data Type when assigning variables. So for example, if you had a variable for logging peoples age... you would choose the Byte Variable Type, as you wouldn't expect anyone to live over the age of 255! i.e. the maximum number a Byte can store. 


Furthermore, you wouldn't try and store today date, in a Long data type... as they are incompatible. VBA will normally prompt you with an error if you get things wrong. So don't worry about it to much to start with. Just have a read of the different data types in the table, you'll see them crop up in future code examples



3.3 Variable Naming Rules

So we've learnt a bit about variable names and Data types. There are some rules and regulations you need to be aware of though. When creating a variable name, it:

  1. Must not begin with a number
  2. Must not contain any spaces
  3. Must not exceed 40 characters in length
  4. May contain only letters, numbers of the underscore symbol

That concludes, this brief introduction to variables. Next up, we'll be learning a bit about Code Structure and some of the main coding techniques you need to know about!


Quote of the Day: "Of course I talk to myself... sometimes I need expert advice"

(Click Here to Return to Index)

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...