Excel vba value - Мир ПК

Мир ПК
1 просмотров
Рейтинг статьи
1 звезда2 звезды3 звезды4 звезды5 звезд

Excel vba value

VBA Value

Excel VBA Value Property

Value is a property in VBA which is mostly used with the range method to assign a value to a specific range, it is an inbuilt expression in VBA, for example, if we use range(“B3”).value = 3 this will assign cell B3 a value of 3, not necessarily that value property is to be used with only range method we can use it with other functions as well.

Early on in our learning with VBA we are so curious to learn about how to store the data in the cells. If you are that curious then you need to understand “Value”property. In this article, we will explain to you about “Value” property, how to insert or set values, how to get value from the cell and many other things.

In our one of the earlier articles, we have discussed “VBA Range Cells”. Range object can help us to refer to a single cell as well as multiple cells. To use RANGE object first we need to decide for which cell we need to insert the value and what is the value we are going to insert.

How to use Value Property in VBA?

Example #1 – Range Object to Assign Values to Cells

For example, if you want to insert a value to the cell A1 then you should refer the cell A1 like this Range(“A1”)


After referring the cell using RANGE object now put a dot (.) to see the IntelliSense list of all the properties and method associated with this object.


Form this variety of options select the property “VALUE”.


Once the “VALUE” property selected we need to set the value to the cell A1 by putting the value in equal sign.


Ok, this will insert the value “Welcome to VBA” to the cell A1.

If you want to insert the same value to multiple cells then refer the cells like below code.


This will insert the value from the cell A1 to A5.

If you want to insert values to different cell but not for series of the cell then we need to use code and the cell address in separate arguments like the below.


This will insert the text “Welcome to VBA” to the cells A1, A5, B4, and C2 cells.

Example #2 – Insert Value using CELLS Property

Not through RANGE object but also using VBA CELLS property we can insert values. But one of the problems with CELLS object is we don’t get the access of IntelliSense list as we got for RANGE object.

Here we need to mention the row & column numbers we need the insert the value. For an example if you want to insert the value to the cell A1 then the code is CELLS(1,1), if you want to insert the value to the cell B5 then the code is CELLS(5,2) i.e. equal to B5 cell.

We cannot insert values to multiple cells by using CELLS property, this is unlike our RANGE object.

Example #3 – Get Cell Value

We have seen how to insert values to the cells, now we will see how to get VBA values from cells.

Step 1: Define a variable as String.


Step 2: For this variable “k” we will assign the value of the cell A1. In cell A1 I have entered the value “Welcome to VBA”.

So the code will be k = Range(“A1”).Value


Step 3: Show the result of the variable “k” in the VBA message box.

Читать еще:  Excel worksheet change


By running the code we should the result of cell A1 value in the message box.

We can also use the RANGE object to get the data of the cell A1, below code will show you the same.


This should also get the value of the cell A1 in the message box.

Example 4 – Error Value if More than One Cell Value Requires

For an example look at the below code.


If you run the above code we will get “Type Mismatch” error in VBA.

The reason why we get this error because when the object variable set to more than one cell “value” property doesn’t really understand which cell value to be given, so it can get single cell value at a time.

Recommended Articles

This has been a guide to VBA Value Property. Here we learn how to get the VBA object cell value and range to assign values to cells along with examples and downloads to excel template. Below are some useful excel articles related to VBA –

Excel vba value

������� ���������� ��������� �� ����� VBA – �������, ����� �������� �� ���������� ������, � ����������� �� ��������� ����������. ���� ���������� �������� ���������, ������� ����� ����������� ������������ ���������� ��� ��������, ����������� �������������� ����������� ������������ ���������� Microsoft Office, �� ���������� ������� ����������� ������. ��� ���������� ���� ��������� ����� ��������� � ������� ������� ���������� ������, ������� ������� ����� �������� ���������� ���������. ��� ����� � ���������� ���������� �������� ������ ������������ � ������� ������� ��� ������ ������������ �������� ����������, � ����� ������� ������ � ��������������� ����������� �����. ���� ��������� ��������� � ������� ������� ������ ������ �������.

���������� “�����������” ��������� (��� ���������� ������� ��������� ��������� ����, � ���������� ���������� ����������) ����� �������� ��� �����. ������ ���� – ���� ����������� ����������������, �� ������� ��������� ���� (�����) ���������, ��� ������������� ����������� �������� ����������. ������ – ���� ����������������, �� ������� ��������� ����� ��������� (���������), ������������� � ����� �� ������������ �������. �������� ��������, ��������, ������ ����� ������� ���� �� ��������� ������ (������� Click), ������� ������� �� ���������� (������� KeyPress) � �.�. ������������ ����� ���������� ����� �������� ������ – «������ �������».


Cells(i, j)

Rows(� ������)

Columns(� �������)


Sheets(� �����)


Range(“A5:A10”). Value = 0 ��� Range(“A5:A10”) = 0 – � �������� ����� A5:A10 ��������� �������� 0.

Cells(2, 4). Value = n ��� Cells(2, 4) = n – � ������, ����������� �� ����������� 2-� ������ � 4-�� ������� (������ � ������� “D2”), ��������� �������� ���������� n.

Xn = Cells(1, 2).Value ��� Xn = Range(“B1”).Value – ���������� Xn ������������� �������� �� ������ B1 �������� �������� �����.

Sheets(2).Activate ������� �������� ���� � �2.

Sheets(“���������”).Delete ������� ���� � ������ “���������”.

Range(«A5:A10»).Clear – �������� �������� ����� A5:A10.

Range(«A2:B10»).Select – �������� �������� ����� A2:B10.

Application.Quit — ���������� ������ � Excel.

Spreadsheets Made Easy

VBA Ranges – Getting and Setting Cell Values

In the previous post, we introduced the VBA Range object. This gave us the foundation of working with Ranges in VBA. In today’s post, I’d like to discuss how to get and set cell values through VBA. This will continue to build up our understanding of the VBA Range object and how to use it. There are several ways you can get and set cell values with VBA and I’ll do my best to cover all the necessities, but at the same time keeping it short and to the point. Let’s get started.

Читать еще:  Вкладка сервис в excel 2020

Getting Cell Values

To get a cell’s value in VBA, we need to refer to it with the Range object and then call the .Value property.

We’ll use the following spreadsheet for our example. It’s a simple table with some names in it.

To get the value from cell A2 you can use this code snippet:

This will take cell A2 and put it in the variable val . Then we print out the value in the Immediate Window (which the value in our example is Joseph ).

You can also set the range to a variable and access the value from that variable as well:

What happens if you use .Value on a set of cells?

Let’s change our previous code snippet to the following:

If you run this code, you will get an error stating that there is a type mismatch.

What’s going on here?

The problem is that when you work with a set of cells, .Value can only return a single value. So when we ask VBA to return .Value on our variable (which refers to multiple cells), the .Value property doesn’t know which cell we are referring to.

How do you get a single cell from a set of cells?

In order to use .Value to get a value from a cell, we need to refer to a single cell from the range of cells in our variable. The way we do that is with the Cells() VBA function.

The Range.Cells Function

The Cells() function is a way to take a range of cells and return a single cell from the set. Here is the function defined:

How do you set multiple cells’ values?

Remember how I said that you can only read from one cell using .Value ? Well, when setting values, you can actually set multiple cells at one time by using .Value . Take a look at the following code:

If you ran this code, it would set all A2:A5 ‘s cells to John :

Well, maybe you’d actually want to do this for some other scenarios, like when you want a bunch of cells to repeat a value.

Let’s take a real example for a second. Let’s say we have two columns, First Name and Last Name . We want to take the Last Name column and place its value after the First Name ‘s value; essentially combining the values to make a single Name column.

Here’s our sample data:

Our task is to combine the first and last name columns and place the result in column A . How do we do that?

One solution is to loop through cells A2 through A5 and then set that cell’s value to its own value, plus a space, plus the last name of the cell right next to it.

Sounds easy enough, let’s code it up:

Let’s step through the code.

  • First, we create a variable called names . Then, we set that to range A2:A5 .
  • Next, we create a variable called cell . This is going to be a temporary variable that will change with each iteration of the loop.
  • Then, we create the loop. Here, we’re looping through the names range object and setting the current item to the cell variable. This means that each time we run through the loop, cell represents a single range object.
    • The first time the loop is run, cell is set to A2 . Then, A3 , next A4 , and finally A5 . After that, there are no more cells to go through in the names variable, so the loop ends.
    • I’ll go over how to loop through ranges in a future post since this post is already long enough!
  • Now we’re ready to combine the first and last names. How we do that is with another Range function called Offset(rows, columns) . The idea with this function is that if you’re on a cell like A2 and you say cell.Offset(0, 1) what we’re really saying is “move over one column to the right”. This puts us on cell B2 . That’s how we’re able to get the last name in our example.
    • I’ll discuss how to use the Offset() function in more detail in a future post. Again, this post has gone on long enough

Here are the results of the code after we run it:

From here, we could change the A1 cell to just Name and delete column B altogether.

Getting and Setting Cell Values from a Named Range or Table Name

One last thing I’d like to touch on is when you use the Range() function, you can use a named range or table name instead of a range like A2:A5 . In our first example, our data is in a table named Table1 . To refer to the data of the table, we could use the following:

And to refer to the entire table, we can leverage structured references like so:

This will return A1 ‘s value “Name” since the table starts in A1 .

Also, if you’re new to Excel Tables, click here to learn more.

What’s next?

Honestly, there is so much to discuss with range objects in VBA. I’ll be touching on many more topics regarding ranges in VBA in upcoming posts such as:

  • Modifying cell colors
  • Finding cells by their text values
  • Filtering data
  • Getting the last row in a range (you need this more often than you think)

I’ll come back to this post and put links to these posts as I create them.

If you enjoyed this content, please share and subscribe!

Ссылка на основную публикацию