How to Get Cell Value as String Using Excel VBA (4 Easy Ways)

Here’s an overview of the VBA code needed to get cell values.

Overview image of VBA get cell value as a string.png


How to Get Cell Value as String with Excel VBA: 4 Approaches

Method 1 – Get a String Cell Value from the VBA Variable Type


Case 1.1 – Use a String Variable

We will show you how to get a cell value declaring a string-type variable. Here’s a dataset from where we will get our cell values.

The dataset of VBA to get cell value as a string.

  • Launch the VBA window and insert a module.
  • Paste the below code:
Sub Get_Cell_Value_String()
Dim CellValue As String
CellValue = Range("B5").Value
Debug.Print CellValue
End Sub
  • Click on the Play button or press F5.

Applying VBA code to get cell value as a string.


Case 1.2 – Apply a Variant Type Variable

  • Add a VBA module in Microsoft Excel.
  • Paste the below code in the module.
Sub Get_Cell_Value_Variant()
Dim CellValue As Variant
CellValue = Range("B6").Value
Debug.Print CellValue
End Sub
  • Click on the Play button or press F5.

We found “Sarah” as output since the value is in cell B6.

Applying Variant type variable


Method 2 – Use the Range or Cells Property

  • Launch VBA and insert a Module.
  • Paste the below code:
Sub Get_Cell_Value_select()
Range("B7").Select
End Sub

In the VBA code, we wrote cell B7 as the range, so the cell B7 has been selected.Selecting a cell in an Excel sheet through VBA


Method 3 – Get a Value from One Cell to Another Cell

  • Launch VBA and insert a Module.
  • Insert the following code:
Sub Get_Cell_Value_copy()
Range("B14").Value = Range("B11").Value
End Sub
  • Click on the Play button or press F5.

This will get the value from cell B11 to B14 using VBA code.

Getting a value from one cell to another cell

Read More: Excel VBA: Get Cell Value from Another Workbook without Opening


Method 4 – Convert the Cell Value as String


Case 4.1 – Integer to String

  • Launch VBA and insert a Module.
  • In the Module, paste the following code:
Sub integer_to_string()
Dim A As Integer
Dim B As String
A = Range("F5").Value
B = CStr(A)
Debug.Print B
Debug.Print (TypeName(B))
End Sub
  • Press the Run button.

You will see two outputs in the immediate window. The second output “String” means that the first output “24” convert into a string variable.

Converting integer to string


Case 4.2 – Single/Double to String

  • Launch VBA and insert a Module.
  • Paste the following code into the module.
Sub Double_to_string()
Dim A As Double
Dim B As String
A = Range("F5").Value
B = CStr(A)
Debug.Print B
Debug.Print (TypeName(B))
End Sub
  • Click on the Play button or press F5 to run the code.

Two outputs will be visible in the window. The first output, “23.9” is transformed into a string variable by the second output, “String“.

Converting double to string


Case 4.3 – Date to String

We are going to turn the date variable in cell C5 into a string variable by using the VBA macro.

  • Launch VBA and insert a Module.
  • Paste the following code into the module:
Sub Date_to_string()
Dim A As Date
Dim B As String
A = Range("C5").Value
B = CStr(A)
Debug.Print B
Debug.Print (TypeName(B))
End Sub
  • Run the code by pressing the F5 key or by clicking on the play button to see the result.

The second output, “String” shows the first output, “12/1/1998” converted into a string variable.

Converting the Date variable to a string


How to Set the Cell Value as String Using VBA in Excel


Method 1 – Setting a Cell Value as a Variable

  • Launch VBA and insert a Module.
  • Insert this code:
Sub Set_string_1()
strtext = "ExcelDemy"
Range("A13").Value = strtext
Debug.Print strtext
End Sub
  • Click on the Play button or press F5.

You will see “ExcelDemy” as an output in the immediate window and on your sheet’s cell A13.

Setting cell values as strings


Method 2 – Setting Multiple Cell Values at Once

  • Use this code in a VBA module:
Sub Set_string_2()
Range("B4:D10").Value = "1"
End Sub
  • Click on the Play button or press F5.

We put 1 inside a quotation mark(“”). It’s because 1 is an integer. If we put 1 in between another set of quotation marks it will act as a string.

So as output you can see the cell values of cell B4:D10 change into “1”.

Setting multiple cell values


Things to Remember

  • Cell Format: The format of a cell can affect how its value is displayed. When retrieving cell values as strings, be aware that the formatting may impact the output. For instance, if a cell is formatted as a date, the retrieved string value might be in a date format rather than the desired string representation. You can consider adjusting the cell format or using the “Text” property of the cell to obtain the displayed value.
  • Empty Cells: If a cell doesn’t contain any value, attempting to retrieve it as a string may result in an empty string. To handle such cases, you can check if the cell is empty using VBA’s “IsEmpty()” function or test the length of the retrieved string value to ensure it contains meaningful data.
  • Range Selection: Accurate specification of the range is crucial when retrieving cell values to prevent errors or unintended outcomes. Double-check the range references and ensure they correspond to the desired cells or ranges in your worksheet.

Frequently Asked Questions

How to extract value from a string in VBA?

To extract a value from a string in VBA, you can use various string manipulation functions and techniques. Here are a few common approaches:

  • Using Left, Mid, or Right functions
  • Using Split function
  • Using regular expressions

How can I get the value of a named range in VBA?

To get the value of a named range in VBA, you can refer to the range using its name and then retrieve its value. Here’s an example:

Dim cellValue As String
cellValue = CStr(Range("MyNamedRange").Value)

In this code, “MyNamedRange” is the name of the range, and the Value property retrieves its value as a string.

How to combine string and cell values in VBA?

To combine a string and a cell value in VBA, you can use the concatenation operator (&) or the Concatenate function.

Dim stringValue As String
Dim cellValue As String
stringValue = "The value in cell A1 is: "
cellValue = CStr(Range("A1").Value)
Dim combinedString As String
combinedString = stringValue & cellValue
MsgBox combinedString

Download the Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Mizbahul Abedin
Mizbahul Abedin

Md Mizbahul Abedin, BSc, Textile Engineering and Management, Bangladesh University of Textiles, has been working with the ExcelDemy project for 11 months. Currently working as an Excel and VBA Content Developer who provides authentic solutions to different Excel-related problems and writes amazing content articles regularly. He has published almost 20 articles in ExcelDemy. He has passions for learning new things about Microsoft Office Suite and Data analysis. Besides, he also likes to travel, photography, international politics, and read... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo