Variables and Data Types
Swipe to show menu
Every procedure you write needs somewhere to hold values while it works — a customer's name, a running total, a yes/no flag. That's what a variable is: a labeled box in memory that you can put a value into and read back later. You create one with Dim (short for "dimension"), and you tell VBA up front what kind of thing it will hold:
Dim productName As String
Dim currentStock As Integer
Dim unitPrice As Double
Each line reserves a labeled slot: productName can only ever hold text, currentStock only whole numbers, unitPrice only numbers with decimals. Declaring the type up front isn't bureaucracy for its own sake — it's what lets VBA catch mistakes before they become bugs. Try to run productName = 47 and VBA stops you immediately with a type mismatch error, rather than silently storing something wrong and letting it cause confusion three steps later.
Common Data Types
A rule of thumb: use Long instead of Integer for counters and row numbers — it's not slower in modern Excel, and it won't overflow on large datasets. You'll see Dim i As Long everywhere in this course.
Option Explicit
Put this single line at the very top of every module, before any Sub. It changes one thing: VBA will now refuse to run any line that uses a variable you haven't declared with Dim first:
Option Explicit
Sub Example()
Dim total As Double
total = 100 ' works — declared above
toatl = 200 ' typo — Option Explicit catches this at compile time
End Sub
Without Option Explicit, that typo silently creates a brand-new variable called total, and you'd spend twenty minutes wondering why total never changes. This single habit prevents more bugs than anything else in this chapter.
Constants
Constants are named values that never change while the code runs — perfect for things like tax rates or thresholds:
Const REORDER_THRESHOLD As Integer = 20
Using REORDER_THRESHOLD instead of typing 20 everywhere means if the business rule changes, you edit one line instead of hunting through your code.
Task
- In
Section_2_VBA_Fundamentals.xlsm, write aSubthat declares aStringvariable for a product name, aDoublefor its price, and anIntegerfor its stock. - Assign them the values for
USB-C Hubby typing the literal values in. - Use
MsgBoxto display all three in one message, concatenated together. - Add
Const LOW_STOCK As Integer = 20above yourSuband use it in aMsgBoxcomparing it to the stock variable.
- You need three
Dimlines with different types — refer back to Figure 2.1 if you forget the syntax:Dim name As Type. - USB-C Hub's row is Price 39.50, Stock 44. Just assign these directly:
price = 39.50. - Concatenation uses
&, not+. Build the string in pieces:"Name: " & productName & ", Price: " & price. - Constants are declared outside any
Sub, at the top of the module, right afterOption Explicit. ComparestockCounttoLOW_STOCKwith a plain>or<inside aMsgBoxstring — you don't need anIfyet, just show both values so you can eyeball the comparison.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat