Excel VBA: Set Variable to a Cell Value (3 Practical Examples)

We will use the sample dataset below to show how to set variables to cell values using VBA.

Data Set to Set Variable to a Cell Value in Excel


Example 1 – Set Variable to a Single Cell Value Using Excel VBA

Set the value of cell B4 to a variable called Customer_Name.

It’s better if you know the data type of the value and declare it beforehand, and set the value in that variable.

Cell B4 contains the name, Boris Pasternak. It’s a string-type variable. Therefore, you can use the following 2 lines of codes:

Dim Value As String
Value = Range("B4")

VBA Code to Set Variable to a Cell Value in Excel

Use only the 2nd line if you don’t know the data type. VBA will read the value correctly for you.

Value = Range("B4")

To check whether VBA has read the value correctly, enter the line MsgBox Value with either of the two sets of codes and run the code.

A message box will appear showing you the value of cell B4, Boris Pasternak.

Output to Set Variable to a Cell Value in Excel


Example 2 – Set Values of an Adjacent Range of Cells into a Variable Using Excel VBA

Let’s read the range B3:D13 into the variable Rng.

The data type of the variable will be a Range. You can either declare it beforehand like this:

Dim Rng As Range
Set Rng = Range("B3:D13")

Or you can directly assign the values with it.

Set Rng = Range("B3:D13")

To access the values of the Range object, you have to access them with their row and column numbers with a Cells object.

To access cell B4, you have to use Rng.Cells(2,1) [Cell B4 is in the 2nd row and 1st column of the range B3:D13.]

Enter the line MsgBox Rng.Cells(2,1) inside your code and run it. It’ll show Boris Pasternak, the value in cell B4.

Output to Set Variable to a Cell Value in Excel


Example 3 – Set Values of a Non-Adjacent Range of Cells into a Variable Using Excel VBA

Let’s try to read the range B3:B13 and D3:D13 into a variable called Rng.

It’s again a data type of Range. You can either declare it beforehand or not.

We’ll use the Union method of VBA to join 2 or more non-adjacent ranges of cells into a single range.

Dim Rng As Range
Set Range = Union(Range("B3:B13"), Range("D3:D13"))

VBA Code to Set Variable to a Cell Value in Excel

To access cell B4, enter the line MsgBox Rng.Cells(1,1). Run the code, and it’ll display the content in cell B4, Boris Pasternak.


Things to Remember

All the methods described above work for the active worksheet only. If you want to access a worksheet that is not active, you have to mention the name of the worksheet before the range.

To access the range B4 of Sheet1, you have to use:

Value = Worksheets("Sheet1").Range("B4")


Download Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo