Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Variables and Data Types | VBA Fundamentals
Excel VBA for Business Automation

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

  1. In Section_2_VBA_Fundamentals.xlsm, write a Sub that declares a String variable for a product name, a Double for its price, and an Integer for its stock.
  2. Assign them the values for USB-C Hub by typing the literal values in.
  3. Use MsgBox to display all three in one message, concatenated together.
  4. Add Const LOW_STOCK As Integer = 20 above your Sub and use it in a MsgBox comparing it to the stock variable.
Hint
expand arrow
  1. You need three Dim lines with different types — refer back to Figure 2.1 if you forget the syntax: Dim name As Type.
  2. USB-C Hub's row is Price 39.50, Stock 44. Just assign these directly: price = 39.50.
  3. Concatenation uses &, not +. Build the string in pieces: "Name: " & productName & ", Price: " & price.
  4. Constants are declared outside any Sub, at the top of the module, right after Option Explicit. Compare stockCount to LOW_STOCK with a plain > or < inside a MsgBox string — you don't need an If yet, just show both values so you can eyeball the comparison.
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 2. Chapter 1
some-alt