12.6 Variables and Data Types
Declare variables with Dim name As Type.
| Type | Holds | Example |
|---|---|---|
Long |
Whole numbers (use instead of Integer) | Dim orders As Long |
Double |
Decimals | Dim amount As Double |
Currency |
Money with 4 fixed decimals | Dim fee As Currency |
String |
Text | Dim city As String |
Date |
Date/time | Dim orderDate As Date |
Boolean |
True/False | Dim isLate As Boolean |
Variant |
Anything (default if not declared) | Dim v As Variant |
Object types |
Worksheets, ranges | Dim ws As Worksheet, Dim rng As Range |
About this data: Teaching sample for a fictional shop “GreenField Dairy”. Figures and names (e.g. Vikram Rao) are made up for this lesson on variables and data types.
Object variables need Set: Set ws = Worksheets("Orders").
Sub VariablesDemo()
Dim city As String
Dim orders As Long
Dim sales As Double
Dim aov As Double
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Orders")
city = "Pune"
orders = Application.WorksheetFunction.CountIfs(ws.Range("C:C"), city)
sales = Application.WorksheetFunction.SumIfs(ws.Range("G:G"), ws.Range("C:C"), city)
If orders > 0 Then aov = sales / orders
MsgBox city & ": " & orders & " orders, AOV Rs " & Format(aov, "0.00")
End Sub
On the Module 3 mini dataset this shows Pune: 4 orders, AOV Rs 136.00.
Ravindra Bagale's Tip
Many students use Integer for row numbers – Integer's limit is 32,767, and with big data you get an "Overflow" error. Always use Long for rows and counts. If you forget Set for an object variable, you get the "Object variable not set" error – that's very common too.
Ravindra Bagale's Tip – मराठी
Row numbers साठी बरेच students Integer वापरतात – Integer ची मर्यादा 32,767 आहे, आणि मोठा data आला की "Overflow" error येतो. Rows आणि counts साठी नेहमी Long वापरा. Object variable ला Set लावायला विसरलं तर "Object variable not set" error येतो – हा पण खूप common आहे.
Ravindra Bagale's Tip – हिंदी
Row numbers के लिए बहुत से students Integer इस्तेमाल करते हैं – Integer की सीमा 32,767 है, और बड़ा data आते ही "Overflow" error आता है. Rows और counts के लिए हमेशा Long इस्तेमाल करो. Object variable के साथ Set लगाना भूल गए तो "Object variable not set" error आता है – यह भी बहुत common है.
Worked example — variables and data types
Why
Vikram Rao in Pune and Liam Murphy abroad both need a clear, repeatable way to use variables and data types on a QuickCart Delivery sheet without breaking totals.
What
Variables And Data Types is the Excel idea this page teaches. We will walk one tiny example end to end.
How
- Open a blank sheet. In A1 type a short label for variables and data types.
- Enter a small made-up value for Vikram Rao using ₹ where money appears.
- Add a second value for Liam Murphy using £ so you see mixed currencies stay numeric.
- Apply the method from this lesson step by step (menu or formula as shown above).
- Check the result still works inside SUM or a simple check cell.
Formula / action
Follow the formula or ribbon steps shown in this lesson (keep values numeric).
Meaning: Once variables and data types is correct on one row, copy the same pattern down the column.
Practice tasks (10+)
Try these on a blank sheet. Open a hint only if you are stuck. No quiz — just practice.
1. Rebuild the smallest example of variables and data types on a blank sheet for GreenField Dairy.
Hint: Use only the steps on this page. Keep numbers numeric.
2. Change one input for Priya Deshmukh and confirm the result updates.
Hint: If the result is stuck, check absolute vs relative references.
3. Repeat the same steps with a £ or € value for Emma Clarke.
Hint: Formatting must not break SUM.
4. Write the result meaning in one plain sentence next to the cell.
Hint: Imagine explaining it to a new joiners.
5. Add a second row and fill the method down.
Hint: Double-click the fill handle if the pattern is consistent.
6. Break the example on purpose, then fix it using this lesson.
Hint: Common break: text instead of a number.
7. Apply variables and data types to a 5-row made-up GreenField Dairy table.
Hint: Keep column headers clear.
8. Check your answer with a simple total or COUNT.
Hint: A wrong total usually means a format or key mismatch.
9. Swap ₹ and $ on one column and re-check totals.
Hint: Stored values stay the same; only the display changes.
10. List two mistakes beginners make with variables and data types.
Hint: Look back at the tip box on this page.
11. Teach the steps out loud in under one minute.
Hint: Why → What → How.
12. Save the sheet as GreenFieldD_practice.xlsx.
Hint: Use a clear file name for class.
Loading practice tasks...
- Last used row (Easy): Which row number is the last filled row? The headings are in row 1.
- Late orders (Easy): How many rows have Status "Late"?
- Total amount (Easy): Add up the Amount column.
- Worst delay (Easy): What is the largest number in the Days late column?
- Late orders in Pune (Medium): How many orders are Late and in City "Pune"?
- Customer for an order (Medium): Find the Customer for Order ID "DL-704".
- Money stuck in late orders (Medium): Add up the Amount column for rows where Status is "Late".
- Big orders (Medium): How many rows have an Amount of 2000 or more?
- Message text (Medium): Build the text "N late orders", where N is the count of Late rows.
- Average delay of late orders (Hard): What is the average Days late for rows with Status "Late"? Round to 1 decimal.
- Tag for the first row (Hard): For the first row, return "Chase" if Days late is 3 or more, else "OK".
- Warning or all clear (Hard): Return "Warning" if any order is 3 or more days late, else "All clear".
Quick revision cards
Three standalone cards. Each one explains the idea with its own example. Scan the QR to open this lesson.


