Getting a value from an excel cell vba

In this Article

  • Set Cell Value
    • Range.Value & Cells.Value
    • Set Multiple Cells’ Values at Once
    • Set Cell Value – Text
    • Set Cell Value – Variable
  • Get Cell Value
    • Get ActiveCell Value
    • Assign Cell Value to Variable
  • Other Cell Value Examples
    • Copy Cell Value
    • Compare Cell Values

This tutorial will teach you how to interact with Cell Values using VBA.

Set Cell Value

To set a Cell Value, use the Value property of the Range or Cells object.

Range.Value & Cells.Value

There are two ways to reference cell(s) in VBA:

  • Range Object – Range(“A2”).Value
  • Cells Object – Cells(2,1).Value

The Range object allows you to reference a cell using the standard “A1” notation.

This will set the range A2’s value = 1:

Range("A2").Value = 1

The Cells object allows you to reference a cell by it’s row number and column number.

This will set range A2’s value = 1:

Cells(2,1).Value = 1

Notice that you enter the row number first:

Cells(Row_num, Col_num)

Set Multiple Cells’ Values at Once

Instead of referencing a single cell, you can reference a range of cells and change all of the cell values at once:

Range("A2:A5").Value = 1

Set Cell Value – Text

In the above examples, we set the cell value equal to a number (1).  Instead, you can set the cell value equal to a string of text.  In VBA, all text must be surrounded by quotations:

Range("A2").Value = "Text"

If you don’t surround the text with quotations, VBA will think you referencing a variable…

Set Cell Value – Variable

You can also set a cell value equal to a variable

Dim strText as String
strText = "String of Text"

Range("A2").Value = strText

Get Cell Value

You can get cell values using the same Value property that we used above.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

automacro

Learn More

Get ActiveCell Value

To get the ActiveCell value and display it in a message box:

MsgBox ActiveCell.Value

Assign Cell Value to Variable

To get a cell value and assign it to a variable:

Dim var as Variant

var = Range("A1").Value

Here we used a variable of type Variant. Variant variables can accept any type of values.  Instead, you could use a String variable type:

Dim var as String

var = Range("A1").Value

A String variable type will accept numerical values, but it will store the numbers as text.

If you know your cell value will be numerical, you could use a Double variable type (Double variables can store decimal values):

Dim var as Double

var = Range("A1").Value

However, if you attempt to store a cell value containing text in a double variable, you will receive an type mismatch error:

get cell value assign variable

Other Cell Value Examples

VBA Programming | Code Generator does work for you!

Copy Cell Value

It’s easy to set a cell value equal to another cell value (or “Copy” a cell value):

Range("A1").Value = Range("B1").Value

You can even do this with ranges of cells (the ranges must be the same size):

Range("A1:A5").Value = Range("B1:B5").Value

Compare Cell Values

You can compare cell values using the standard comparison operators.

Test if cell values are equal:

MsgBox Range("A1").Value = Range("B1").Value

Will return TRUE if cell values are equal. Otherwise FALSE.

You can also create an If Statement to compare cell values:

If Range("A1").Value > Range("B1").Value Then

  Range("C1").Value = "Greater Than"

Elseif Range("A1").Value = Range("B1").Value Then

  Range("C1").Value = "Equal"

Else

  Range("C1").Value = "Less Than"

End If

You can compare text in the same way (Remember that VBA is Case Sensitive)

A cell is an individual cell and is also a part of a range. Technically, there are two methods to interact with a cell in VBA: the range method and the cell method. We can use the range method like range(“A2”). The value will give us the value of the A2 cell, or we can use the cell method as cells(2,1). The value will also give us the value of A2 cells.

Be it Excel or VBA, we all need to work with cells because it will store all the data in cells. So, it all boils down to how well we know about cells in VBA. So, if cells are such a crucial part of the VBA, then it is important to understand them well. So, if you are a starter regarding VBA cells, this article will guide you on how to get cell values in Excel VBA in detail.

First, we can reference or work with cells in VBA in two ways: using CELLS propertyCells are cells of the worksheet, and in VBA, when we refer to cells as a range property, we refer to the same cells. In VBA concepts, cells are also the same, no different from normal excel cells.read more and RANGE object. Of course, why CELLS is a property and why RANGE is an object is a different analogy. Later in the article, we will get to that point.

Table of contents
  • Get Cell Value with Excel VBA
    • Examples of getting Cell Value in Excel VBA
      • Example #1 – Using RANGE or CELLS Property
      • Example #2 – Get Value from Cell in Excel VBA
      • Example #3 – Get Value from One Cell to Another Cell
    • Things to Remember
    • Recommended Articles

Get-Cell-Value-in-VBA

Examples of getting Cell Value in Excel VBA

Below are the examples of getting cell values in Excel VBA.

You can download this VBA Get Cell Value Excel Template here – VBA Get Cell Value Excel Template

Example #1 – Using RANGE or CELLS Property

In cell A1 we have a value of “India.”

VBA Get cell value Example 1

We can reference this cell with a CELLS property or RANGE object. Let us see both of them in detail.

Using Range Property

First, start the macro procedure.

Code:

Sub Get_Cell_Value()

End Sub

VBA Get cell value Example 1-1

Now open the RANGE object.

Code:

Sub Get_Cell_Value()

Range(

End Sub

VBA Get cell value Example 1-2

The first argument of this object is “Cell1,” which is the cell we are referring to. In this case, it is cell A1, so we need to supply the cell address in double quotes for the RANGE object.

Code:

Sub Get_Cell_Value()

Range("A1")

End Sub

VBA Get cell value Example 1-3

Since only one cell refers to other parameters is irrelevant, close the bracket and put a dot to see the IntelliSense list.

VBA Get cell value Example 1-4

As you can see above, the moment we put a dot, we can see all the available IntelliSense lists of properties and methods of range objects.

Since we are selecting the cell, we need to choose the “SELECT” method from the IntelliSense list.

Code:

Sub Get_Cell_Value()

Range("A1").Select

End Sub

VBA Get cell value Example 1-5

Now, select the cell other than A1 and run the code.

vba get cell value example 1

It does not matter which cell you select when you run the code. It has chosen the mentioned cell, i.e., the A1 cell.

Using Cells Property

Similarly, we use the CELLS property now.

Code:

Sub Get_Cell_Value()

Range("A1").Select
Cells(

End Sub

Example 1-6

Unlike the RANGE object, we could directly supply the cell address. However, using this CELLS property, we cannot do that.

The first argument of this property is “Row Index,” i.e., which row we are referring to. Since we are selecting cell A1 we are referring to the first row, so mention 1.

Example 1-7

The next argument is the “Column Index,” i.e., which column we refer to. For example, the A1 cell column is the first column, so enter 1.

Example 1-8

Our code reads CELLS (1, 1) i.e. first row first column = A1.

Now, put a dot and see whether you get to see the IntelliSense list or not.

VBA Get cell value Example 1-9

We cannot see any IntelliSense list with CELLS properties, so we must be sure what we are writing. Enter “Select” as the method.

Code:

Sub Get_Cell_Value()

Range("A1").Select
Cells(1, 1).Select

End Sub

VBA Get cell value Example 1-10

This will also select cell A1.

Example #2 – Get Value from Cell in Excel VBA

Selecting is the first thing we have learned. Now, we will see how to get value from cells. Before we select the cell, we need to define the variable to store the value from the cell.

Code:

Sub Get_Cell_Value1()

Dim CellValue As String

End Sub

Example 2

Now, mention the cell address using either the RANGE object or the CELLS property. Since you are a beginner, use the RANGE object only because we can see the IntelliSense list with the RANGE object.

For the defined variable, put an equal sign and mention the cell address.

Code:

Sub Get_Cell_Value1()

Dim CellValue As String

CellValue = Range("A1")

End Sub

Example 2-1

Once again, put a dot to see the IntelliSense list.

Intellisense list Example 2-2

From the VBA IntelliSense list, choose the “Value” property to get the value from the mentioned cell.

Code:

Sub Get_Cell_Value1()

Dim CellValue As String

CellValue = Range("A1").Value

End Sub

Example 2-3

Now, the variable “CellValue” holds the value from cell A1. Show this variable value in the message box in VBA.

Code:

Sub Get_Cell_Value1()

Dim CellValue As String

CellValue = Range("A1").Value

MsgBox CellValue

End Sub

Example 2-4

Run the code and see the result in a message box.

vba get cell value example 2

Since there is a value of “INDIA” in cell A1, the same thing also appeared in the message box. Like this, we can get the value of the cell by the VBA valueIn VBA, the value property is usually used alongside the range method to assign a value to a range. It’s a VBA built-in expression that we can use with other functions.read more of the cell.

Example #3 – Get Value from One Cell to Another Cell

We know how to get value from the cell using VBA. Now, the question is how to insert a value into the cell. Let us take the same example only. For cell A1, we need to insert the value of “INDIA,” which we can do from the code below.

Code:

Sub Get_Cell_Value2()

Range("A1").Value = "INDIA"

End Sub

VBA Get cell value Example 3

It will insert the value of “INDIA” to cell A1. Similarly, we can write the code below to get value from one cell to another.

Code:

Sub Get_Cell_Value2()

Range("A5").Value = Range("A1").Value

End Sub

VBA Get cell value Example 3-1

Let me explain the code to you.

For cell A5, we need the value from the cell A1 value; that’s all this code says. So, this will get the value from cell A1 to A5 using VBA codeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more.

VBA Get cell value Example 3-2

Things to Remember

  • Inserting value to cells and getting value from the cell requires the VBA “VALUE” property to be used.
  • We can select only one cell using the CELLS property but use the RANGE object. Likewise, we can select multiple cells.

Recommended Articles

This article has been a guide to Get Cell Value in Excel VBA. Here, we discuss the examples of getting cell values using a range of cell properties in Excel VBA and a downloadable Excel template. Below you can find some useful Excel VBA articles: –

  • VBA Variable Range
  • Split String into Array in VBA
  • Range Cells in VBA
  • Active Cell in VBA

Key Notes

  • The value property can be used in both ways (you can read and write a value from a cell).
  • You can refer to a cell using Cells and Range Object to set a cell value (to Get and Change also).

To set a cell value, you need to use the “Value” property, and then you need to define the value that you want to set. Here I have used some examples to help you understand this.

1. Enter a Value in a Cell

Let’s say you need to enter the value “Done” in cell A1. In that case, the code would be something like the below:

Range("A1").Value = "Done"

As you can see, I have first defined the cell address where I want to add the value, and then the value property. In the end, I have assigned the value “Done” using an equal “=” sign enclosed in double quotation marks.

You can also use the “Cells” property, just like the following code.

Cells(1, 1).Value = "Done"

The above code also refers to cell A1.

Apart from this, there is one more way that you can use and that’s by not using the value property directly assigning the value to the cell.

Cells(1, 1) = "Done"

But this is recommended to use the value property to enter a value in a cell.

Now let’s say you want to enter a number in a cell. In that case, you don’t need to use double quotation marks. You can write the code like the following.

Range("A1") = 99

You can also DATE and NOW (VBA Functions) to enter a date or a timestamp in a cell using a VBA code.

Range("A1").Value = Date
Range("A2").Value = Now

And if you want to enter a value in the active cell then the code you need would be like:

ActiveCell.Value = Date

2. Using an Input Box

If you want a user to specify a value to enter in a cell you can use an input box. Let’s say you want to enter the value in cell A1, the code would be like this:

Range("A1").Value = _
InputBox(Prompt:="Type the value you want enter in A1.")

In the above code, the value from cell A1 assigns to the value returned by the input box that returns the value entered by the user.

3. From Another Cell

You can also set cell value using the value from another cell. Let’s say if you want to add value to cell A1 from the cell B1, the code would be:

Range("A1") = Range("B1").Value

You can also refer to cell B1 without using the value property.

Range("A1") = Range("B1")

4. Set Value in an Entire Range

Imagine you want to enter values in multiple cells or a range of cells instead of a single cell, in that case, you need to write code like the below:

Range("A1:A10").Value = Date
Range("B1, B10").Value = Now

In the first line of code, you have an entire range from cell A1 to A10, and in the second line, there are two cells B1 and B10.

Get Cell Value

As I said, you can use the same value property to get value from a cell.

1. Get Value from the ActiveCell

Let’s say you want to get the value from the active cell, in that case, you need to use the following code.

ActiveCell.Value = Range("A1")

In the above code, you have used the value property with the active cell and then assigned that value to cell A1.

2. Assign to a Variable

You can also get a value from a cell and further assign it to a variable.

Now in the above code, you have the variable “i” Which has the date as its data type. In the second line of the code, the value from cell A1 is assigned to the variable.

3.  Show in a MsgBox

Now imagine, you want to show the value from cell A1 using a message box. In this case, the code would be like the below.

MsgBox Range("A1").Value

In the above code, the message box will take value from cell A1 and show it to the user.

Change Cell Value

You can also make changes to a cell value, and here I have shared a few examples that can help you to understand this.

1. Add a Number to an Existing Number

Let’s say if you want to add one to the number that you have in cell A1, you can use the following code.

Range("A1").Value = Range("A1").Value + 1

The above code assigns value to cell A1 by taking value from cell A1 itself and adding one to it. But you can also use VBA IF THEN ELSE to write a condition to change only when there is a number in the cell.

If IsNumeric(Range("A1").Value) Then
  Range("A1").Value = Range("A1").Value + 1
End If

2. Remove First Character from Cell

Now, the following code removes the first character from the cell value and assigns the rest of the value back to the cell.

Range("A1").Value = Right(Range("A1").Value, Len(Range("A1").Value) - 1)

More Tutorials

    • Count Rows using VBA in Excel
    • Excel VBA Font (Color, Size, Type, and Bold)
    • Excel VBA Hide and Unhide a Column or a Row
    • Excel VBA Range – Working with Range and Cells in VBA
    • Apply Borders on a Cell using VBA in Excel
    • Find Last Row, Column, and Cell using VBA in Excel
    • Insert a Row using VBA in Excel
    • Merge Cells in Excel using a VBA Code
    • Select a Range/Cell using VBA in Excel
    • SELECT ALL the Cells in a Worksheet using a VBA Code
    • ActiveCell in VBA in Excel
    • Special Cells Method in VBA in Excel
    • UsedRange Property in VBA in Excel
    • VBA AutoFit (Rows, Column, or the Entire Worksheet)
    • VBA ClearContents (from a Cell, Range, or Entire Worksheet)
    • VBA Copy Range to Another Sheet + Workbook
    • VBA Enter Value in a Cell (Set, Get and Change)
    • VBA Insert Column (Single and Multiple)
    • VBA Named Range | (Static + from Selection + Dynamic)
    • VBA Range Offset
    • VBA Sort Range | (Descending, Multiple Columns, Sort Orientation
    • VBA Wrap Text (Cell, Range, and Entire Worksheet)
    • VBA Check IF a Cell is Empty + Multiple Cells

    ⇠ Back to What is VBA in Excel

    Helpful Links – Developer Tab – Visual Basic Editor – Run a Macro – Personal Macro Workbook – Excel Macro Recorder – VBA Interview Questions – VBA Codes

    VBA Get Cell Value

    Excel VBA Get Cell Value

    In VBA there are different ways to apply the same logic to get the cell values and for this, we have a basic function called as CELLS where we can choose the coordinates to get the value stored in those cells or cell range. We all have used the method of selecting the range of using RANGE(“cell position”). VALUE. Similarly, we have other methods to get the value stored in different selected cells. Excel VBA Get Cell Value is one of the simplest and basic functions which all of us should know how to use it.

    If we see the syntax, the CELLS function only requires the Row index and Column index coordinates where we just need to put the position of cells.

    VBA Get Cell Value Syntax

    Examples of Get Cell Value in VBA Excel

    We will learn how to use a Get Cell Value in Excel by using the VBA Code with the help of given examples.

    You can download this VBA Get Cell Value Excel Template here – VBA Get Cell Value Excel Template

    Example #1

    Let us consider a cell which is B2 with the cell content as “TEST” as shown below. For this, follow the below steps:

    VBA Get Cell Value Example 0-1

    Step 1: Insert a new module inside Visual Basic Editor (VBE). Click on Insert tab > select Module.

    Insert Module

    Step 2: Write the subprocedure of VBA Get Cell Value.

    Code:

    Sub VBA_GetCellValue1()
    
    End Sub

    VBA Get Cell Value Example 1-1

    Step 3: Now directly use the message box and in the use CELLS with the coordinates as per B2 cell which comes at 2nd row and 2nd column.

    Code:

    Sub VBA_GetCellValue1()
    
    MsgBox Cells(2, 2)
    
    End Sub

    VBA Get Cell Value Example 1-3

    Step 4: Run the code by pressing F5 or Play Button is mentioned below the menu bar. We will see the message which will pick up the value from cell B2 as shown below.

    VBA Get Cell Value Example 1-4

    Example #2

    Let us see another simple code to get the cell value. We will use the same cell B2 as used in example-1. In this example, we will see how to move the cursor to the required cell. For this, follow the below steps:

    Step 1: For this again open a new module and write the subprocedure for VBA Get Cell Value.

    Code:

    Sub VBA_GetCellValue2()
    
    End Sub

    VBA Get Cell Value Example 2-1

    Step 2: Now use Range along with Select function to move the cursor.

    Code:

    Sub VBA_GetCellValue2()
    
    Range("B2").Select
    
    End Sub

    Range Example 2-2

    Step 3: Now move cursor away from cell B2 and then run the code, we will see the cursor is now moved back to cell B2.

    VBA Get Cell Value Example 0-1

    Step 4: This is how we can select the cell with and without cell value. There is one more way to get this one. For this, we will be using CELLS function and putting the same coordinates as used in example-1. The same procedure could be done using the CELLS function.

    Code:

    Sub VBA_GetCellValue2()
    
    Cells(2, 2).Select
    
    End Sub

    VBA Get Cell Value Example 2-4

    Example #3

    In this example, we will see how to get the cell value in a message using a different type of method. For this, follow the below steps:

    Step 1: For this, again open Module and write the sub procedure.

    Code:

    Sub VBA_GetCellValue3()
    
    End Sub

    VBA Get Cell Value Example 3-1

    Step 2: In the name of VBA Get Cell Value as shown below. And in that, first, define a variable as String using DIM.

    Code:

    Sub VBA_GetCellValue3()
    
    Dim Value As String
    
    End Sub

    String Example 3-2

    Step 3: Using the defined variable VALUE, choose the value from the range cell B2.

    Code:

    Sub VBA_GetCellValue3()
    
    Dim Value As String
    Value = Range("B2").Value
    
    End Sub

    VBA Get Cell Value Example 3-3

    Step 4: Use the message box to see the value stored in cell B2.

    Code:

    Sub VBA_GetCellValue3()
    
    Dim Value As String
    Value = Range("B2").Value
    MsgBox Value
    
    End Sub

    VBA Get Cell Value Example 3-4

    Step 5: Once we Run the code by pressing F5 or Play Button is mention below the menu bar, we will get the message box with value as TEST which is at cell B2.

    Message Box Example 3-4

    Step 6: Similar code, can be used if we select the data type as VARIANT instead of STRING. Whereas VARIANT in VBA allows numbers and text both in it.

    Code:

    Sub VBA_GetCellValue3()
    
    Dim Value As Variant
    Value = Range("B2").Value
    MsgBox Value
    
    End Sub

    Variant Example 3-5

    Example #4

    There is another way to use Get Cell Value in VBA which is also another simplest way. For this, follow the below steps:

    Step 1: For this again open a new module and select the cell range from where we want to put. Let say we want to use the same cell range B2 which we have been using before examples.

    Code:

    Sub VBA_GetCellValue4()
    
    Range("B2").Value
    
    End Sub

    VBA Get Cell Value Example 4-1

    Step 2: Now in manner use the cell where we want to put the value. And we are considering the cell A1 here.

    Code:

    Sub VBA_GetCellValue4()
    
    Range("B2").Value = Range("A1").Value
    
    End Sub

    Range Example 4-2

    Step 3: Now run the code to see the output. The value from cell B2 will be moved to cell A1.

    VBA Get Cell Value Example 4-3

    Pros of VBA Get Cell Value

    • This is the basic operation where can easily get cell values from anywhere.
    • It is very easy to copy or move the cell value or cursor to any place we want.

    Things to Remember

    • CELLS function is easy to implement by just putting the co-ordinates of ROW and COLUMN respectively.
    • VBA Get Cell Value helps us to choose the cell value or put the cursor to the desired position.
    • VBA Get Cell Value individually may not be much useful but we can use this operation with different types of code to get valuable results.
    • Once done with code, please save the code in Macro Enable excel format to avoid losing the code.
    • VBA Get Cell Value can be used with CELLS function and with RANGE function as well.

    Recommended Articles

    This is a guide to the VBA Get Cell Value. Here we discuss how to use Get Cell Value in excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –

    1. How to Use VBA Login?
    2. VBA Month | Examples With Excel Template
    3. How to Use Create Object Function in VBA Excel?
    4. How to Use VBA IsError Function?

    We use VBA to automate our tasks in excel. The idea of using VBA is to connect the interface of excel with the programming. One of the very most connections between them is by changing the cell values. The change in cell value by programming shows the power of VBA. In this article, we will see how to set, get and change the cell value. 

    Set Cell Value

    Assigning a cell with a value can be achieved by very two famous functions in VBA i.e. Range and Cells function. 

    Range Function in VBA

    The range function helps access the cells in the worksheet. To set the cell value using the range function, we use the .Value

    Syntax: Range(cell_name).Value = value_to_be_assinged. 

    Set the value to a single cell

    If you want to assign ’11’ to cell A1, then the following are the steps: 

    Step 1: Use the Range function, in the double quotes type the cell name. Use .Value object function. For example, Range(“A1”).Value = 11

    Applying-range-function

    Step 2: Run your macro. The number 11 appears in cell A1

    Running-macro

    Set the value to multiple cells at the same time

    Remember the days, when your teacher gives you punishment, by making you write the homework 10 times, those were the hard days, but now the effort has exponentially reduced. You can set a value to a range of cells with just one line of code. If you want to write your name, for example, “Arushi” 10 times, in the range A2 to A11. Use range function. Following are the steps: 

    Step 1: Use the Range function, in the double quotes, write “Start_of_cell: End_of_cell”. Use .Value object function. For example, Range(“A2:A11”).Value = “Arushi”

    Applying-range-function

    Step 2: Run your macro. The text “Arushi” appears from cell A2 to A11 inclusive. 

    Running-macro

    Cells Function in VBA

    The Cells function is similar to the range function and is also used to set the cell value in a worksheet by VBA. The difference lies in the fact that the Cells function can only set a single cell value at a time while the Range function can set multiple values at a time. Cells function use matrix coordinate system to access cell elements. For example, A1 can be written as (1, 1), B1 can be written as (1, 2), etc. 

    Syntax: Cells(row_number, column_number).Value = value_to_be_assign

    For example, you want to set the cell value to “Arushi cleared CA with Rank “, in cell B1. Also, set cell C1, to ‘1’. Following are the steps:

    Step 1: Open your VBA editor. Use cells function, as we want to access cell B1, the matrix coordinates will be (1, 2). Type, Cells(1, 2).Value = “Arushi cleared CA with Rank” in the VBA code. 

    Using-cell-functions

    Step 2: To access cell C1, the matrix coordinates are (1, 3). Type, Cells(1, 3).Value = 1 in the VBA code. 

    Accessing-cell-C1

    Step 3: Run your macro. The required text appears in cell B1, and a number appears in C1. 

    Running-macro

    Setting Cell values by Active cell and the input box

    There are other ways by which you can input your value in the cell in a worksheet. 

    Active Cell 

    You can set the cell value of a cell that is currently active. An active cell is the selected cell in which data is entered if you start typing. Use ActiveCell.Value object function to set the value either to text or to a number. 

    Syntax: ActiveCell.Value = value_to_be_assigned

    For example, you want to assign the active cell with a text i.e. “Arushi is practicing CA”, also want to change the color of the cell to yellow. Following are the steps: 

    Step 1: Use the ActiveCell object to access the currently selected cell in the worksheet. Use ActiveCell.Value function object to write the required text. 

    Using-active-cell-function

    Step 2: Color the cell by using ActiveCell.Interior.Color function. For example, use vbYellow to set your cell color to yellow. 

    Using-active.cell.interior.color-function

    Step 3: Run your macro. The currently selected cell i.e. B1 has attained the requirements. 

    Running-macro

    Input Box

    You can use the input box to set the cell value in a worksheet. The input box takes the custom value and stores the result. This result could further be used to set the value of the cell. For example, set the cell value of A1, dynamically by taking input, from the input box.

    Following are the steps

    Step 1: Open your VBA editor. A sub-procedure name geeks() is created. Use the Range function to store the value given by the input box. 

    Using-range-function

    Step 2: Run your Macro. A dialogue-box name Microsoft Excel appears. Enter the value to be stored. For example, “geeks for geeks”. Click Ok

    Entering-value-to-be-stored

    Step 3: Open your worksheet. In cell A1, you will find the required text is written. 

    Text-written-in-A1

    Get Cell Value

    After setting the cell value, it’s very important to have a handsome knowledge of how to display the cell value. There can be two ways two get the cell value either print the value in the console or create a message box. 

    Print Cell Value in Console

    The console of the VBA editor is the immediate window. The immediate window prints the desired result in the VBA editor itself. The cell value can be stored in a variable and then printed in the immediate window. For example, you are given a cell A1 with the value ’11’, and you need to print this value in the immediate window. 

    Following are the steps

    Step 1: Press Ctrl + G to open the immediate window. 

    Opening-immediate-window

    Step 2: The cell value in A1 is 1

    Value-in-A1

    Step 3: Open your VBA editor. Declare a variable that could store the cell value. For example, Val is the variable that stores the cell value in A1. Use the Range function to access the cell value. After storing the cell value in the val, print the variable in the immediate window with the help of Debug.Print(val) function.

    Printing-variable-in-immediate-window

    Step 4: Run your macro. The cell value in A1 is printed in the immediate window. 

    Running-macro

    Print Cell Value in a Message Box 

    A message box can also be used to show the cell value in VBA. For example, a random string is given in cell A1 of your string i.e. “Arushi studies in Hansraj”. Now, if you want to display the cell value in A1, we can use Message Box to achieve this. 

    Text-present-in-A1

    Following are the steps

    Step 1: Open your VBA macro. Create a message box by using MsgBox. Use the Range(cell).Value function to access the cell value. 

    Creating-a-message-box

    Step 2: Run your macro. A message box appears, which contains the cell value of A1

    Running-macro

    Change Cell Values 

    The value, once assigned to the cell value, can be changed. Cell values are like variables whose values can be changed any number of times. Either you can simply reassign the cell value or you can use different comparators to change the cell value according to a condition. 

    By reassigning the Cell Value

    You can change the cell value by reassigning it. In the below example, the value of cell A1 is initially set to 1, but later it is reassigned to 2

    Following are the steps

    Step 1: Open your VBA code editor. Initially, the value of cell A1 is assigned to 1. This initial value is printed in the immediate window. After that, we changed the value of cell A1 to 2. Now, if we print the A1 value in the immediate window, it comes out to be 2

    Changing-value-in-cell

    Step 2: The immediate window shows the output as 1 and 2. 

    Immediate-window-shows-output

    Changing cell value with some condition

    We can use if-else or switch-case statements to change the cell value with some condition. For example, if your age is greater than 18 then you can drive else you cannot drive. You can output your message according to this condition. 

    Following are the steps

    Step 1: A code is written in the image below, which tells whether you are eligible for a driving license or not. If your age is greater than 18 then cell A1 will be assigned with the value, “You are eligible for the driving license”, else A1 will be assigned with “You are not eligible for driving license”. 

    Text-assigned-to-A1

    Step 2: Run your macro. An input box appears. Enter your age. For example, 19. 

    Running-macro

    Step 3: According to the age added the cell value will be assigned. 

    Cell-value-assigned

    Понравилась статья? Поделить с друзьями:
  • Getsaveasfilename vba excel параметры
  • Getpivotdata в excel что это
  • Getopenfilename vba excel описание
  • Getobject vba excel примеры
  • Getfilename что это excel