Excel VBA Assignment Operator (=)
Complete Guide to Assigning Values to Variables, Cells and Objects in VBA
Introduction to the VBA Assignment Operator
The Assignment Operator (=) is one of the most important operators in Excel VBA.
It is used to assign a value to a variable, cell, property, object or expression result.
In simple words, the assignment operator tells VBA:
For example:
Here, VBA calculates or reads the value 42 and stores it inside the variable age.
1. Basic Syntax of Assignment Operator
The basic syntax is:
Example:
The value "Chirag" is assigned to the variable name.
- Left side = destination
- Right side = value or expression
- = performs the assignment
2. Left Side and Right Side of =
Consider this statement:
VBA processes it conceptually like this:
price * quantity
Step 2: The result is stored in:
total
Therefore:
3. Assigning Text to a Variable
String values are assigned using double quotation marks.
Now the variable contains:
Another Example
The message box displays Delhi.
4. Assigning Numbers
Numeric values can be directly assigned to numeric variables.
VBA stores the values in the corresponding variables.
quantity = 10
price = 125.50
5. Assignment Using an Expression
The right side does not have to be a simple value. It can contain an entire expression.
VBA first calculates:
Then VBA assigns 30 to result.
6. Assignment with Mathematical Calculations
You can use arithmetic operators on the right side of the assignment.
Result:
Using Variables
Output:
7. Assigning a Value to an Excel Cell
The assignment operator is heavily used when working with Excel cells.
This puts Hello into cell A1.
Assign Number
Assign Calculation
Cell A1 will contain:
8. Reading a Cell into a Variable
Assignment also works in the opposite direction.
If A1 contains:
Then:
This is extremely useful when building Excel VBA automation.
9. Copying One Cell to Another
You can use the assignment operator to copy a cell value.
If A1 contains 100, B1 will also contain 100.
Another Example
10. Assigning a Variable to a Cell
The value stored in the variable is written into cell B2.
11. Assignment with Cell Calculations
You can directly calculate values from Excel cells.
If:
Then:
12. Assigning a New Value to an Existing Variable
A variable can be assigned a new value multiple times.
The final value is:
The previous values are replaced.
13. Increasing a Variable
You can use the assignment operator with the existing value of a variable.
The result is:
Increasing by 10
This technique is commonly used inside loops.
14. Decreasing a Variable
Final value:
15. Assignment with the Concatenation Operator
The assignment operator can be combined with the concatenation operator &.
Result:
Notice that = assigns the final result to fullName, while & joins the text.
16. Assigning Dates
You can assign the current date using VBA's Date function.
Current date is stored in the variable.
Current Date and Time
17. Assigning Boolean Values
Boolean variables can contain either True or False.
You can later change it:
18. Assignment Operator with Objects
VBA also uses Set when assigning an object reference.
For normal values, use:
variable = value
For object references, use:
Set objectVariable = object
19. Assignment to Excel Properties
VBA uses the assignment operator to change many Excel properties.
Change Cell Value
Change Font Size
Change Font Bold
Change Column Width
20. Assigning the Result of a Function
A function can return a value, and that value can be assigned to a variable.
The function returns the length of the text and assigns it to textLength.
Using UCase
Output:
21. Assignment Inside an If Statement
Assignment is frequently used inside conditional statements.
Here the assignment operator stores different values depending on the condition.
22. Assignment Inside a Loop
Assignment operators are extremely important when working with loops.
This writes numbers 1 to 5 into cells A1:A5.
23. Real-World Example – Calculate Invoice Total
Let's create a simple invoice calculation using the assignment operator.
This example demonstrates how the assignment operator is used throughout a real Excel automation process.
24. Common Mistakes with the Assignment Operator
Mistake 1 – Forgetting Quotes Around Text
This is incorrect if Chirag is intended to be text.
Correct:
Mistake 2 – Reversing the Assignment
This is invalid because the left side must be something that can receive the value.
Correct:
Mistake 3 – Forgetting Set for Objects
Correct:
25. Is = Used for Comparison or Assignment?
The = symbol can be used for both assignment and comparison depending on where it appears.
Assignment
This assigns 10 to x.
Comparison
Here = checks whether x is equal to 10.
Outside a condition,
= commonly performs assignment.
Inside a condition such as
If, = is used for comparison.
26. Assignment vs Comparison Operator
| Feature | Assignment | Comparison |
|---|---|---|
| Operator | = | = |
| Purpose | Assign a value | Compare two values |
| Example | x = 10 | If x = 10 Then |
| Result | Stores value | True or False |
27. VBA Assignment Operator – Quick Reference
| Example | Meaning |
|---|---|
| x = 10 | Assign 10 to x |
| name = "Chirag" | Assign text |
| total = price * quantity | Assign calculation result |
| Range("A1").Value = 100 | Write value to cell |
| x = Range("A1").Value | Read cell into variable |
| x = x + 1 | Increase x |
| x = x - 1 | Decrease x |
| Set ws = Worksheets("Sheet1") | Assign an object reference |
28. Practice Questions
Try to solve these questions yourself.
-
Create a variable called
ageand assign 42 to it. -
Create a String variable called
cityand assign "Delhi". - Create two variables and calculate their total.
- Read cell A1 into a VBA variable.
- Write a variable's value into cell B1.
- Increase a variable by 5.
- Decrease a variable by 10.
-
Create a full name using the
&operator. - Assign today's date to a Date variable.
-
Create a Worksheet object variable using
Set.
29. Mini Project – Calculate Employee Salary
Let's use the assignment operator in a small practical project.
This simple example demonstrates:
- Reading Excel cells
- Assigning values to variables
- Performing calculations
- Assigning the result to another variable
- Writing the result back to Excel
- Using concatenation to display a message
30. What Should You Learn Next?
Now that you understand the Assignment Operator, the next important concept is how VBA decides which operation should be performed first when multiple operators are used in one expression.
Recommended next topic:
You will learn how VBA evaluates expressions such as:
Understanding operator precedence is essential before moving to more complex VBA expressions.
31. Related VBA Operator Lessons
32. Final Tip
The assignment operator = is used throughout almost every Excel VBA program.
Whenever you see a statement such as:
remember the basic rule:
Mastering this simple concept will make variables, calculations, Excel automation, loops, conditions and VBA projects much easier to understand.
0 Comments