Excel VBA Codes to Format Cell as Text (3 Easy Examples)

How to Use Code in the Visual Basic Editor

  • Go to the Developer tab from the Excel ribbon.
  • Click on the Visual Basic option.

  • In the Visual Basic for Applications window, click the Insert drop-down and select Module.

  • With a new module is opened, write or paste some code there and press F5 to run.

Method 1 – Use of the Range.NumberFormat Property to Format Cells as Text

In cell C5, we have a short date that we’re going to change as text.

Excel VBA Format Cell as Text

  • In the visual basic editor, copy and paste the following code.
Sub FormatCellAsText()
Range("C5").NumberFormat = "@"
End Sub

Excel VBA Format Cell as Text

  • Press F5 to run the code.

Excel VBA Format Cell as Text

Code Explanation:

  • We used the Range Object to select the cell in the worksheet that contain the input.
  • To format the input value as text, we need to put the Number Format value as “@”.

Similarly, by applying the same piece of code we can change different number formats to text.

Excel VBA Format Cell as Text


Method 2 – Refer to the TEXT Function in a VBA Code to Format Cells as Text

Let’s say we have a Long Date in cell B6 that we want to format as text.

Excel VBA Format Cell as Text

  • Put the following code in the visual basic editor to accomplish this.
Sub FormatCellAsText()
Range("C6") = WorksheetFunction.Text(Range("B6"), "'0")
End Sub
  • By running the code with F5, you can convert the long date into a text value. We can also format a cell containing different number formats to text.

Excel VBA Format Cell as Text

Code Explanation:

  • We used the Range Object to select the cells in the worksheet that contain input and output values.
  • The WorksheetFunction object enabled us to use the TEXT function in VBA code.
  • The TEXT function needs 2 arguments-
    value– the input cell reference (in this example B6).
    format_text- we used ” ‘ 0 ” to convert the value to text format.

Read More: How to Format Text in Excel Cell


Method 3 – Use of the VBA Format Function to Format Cells as Text in Excel

  • Using the following code converts a Long Date in cell C5 to a text.
Sub FormatCellAsText()
Range("C6").Value = Format(Range("B6").Value, "'0")
End Sub

Excel VBA Format Cell as Text

Code Explanation:

  • We used the Range Object to select the cells in the worksheet that contain input and output values.
  • The Format function needs 2 arguments-
    expression– the input cell reference (in this example B6).
    format- we used ” ‘ 0 ” to convert the value to text format.

Alternative Code:

Sub FormatCellAsText()
Range("C6").Value = " ' " & Format(Range("B6").Value, "0")
End Sub

Important Notes

  • We added a single quote ( ‘ ) before zero to enter the number format code as ” ‘ 0 ” in the Text and Format functions argument to format a cell to a text value.
  • To view the code used in the sheet, click the right button on the sheet name and select the View Code option.


Download the Practice Workbook


Related Articles


<< Go Back to Text Formatting | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Al Arafat Siddique
Al Arafat Siddique

Al Arafat Siddique, BSc, Naval Architecture and Marine Engineering, Bangladesh University of Engineering and Technology, has worked on the ExcelDemy project for two years. He has written over 85+ articles for ExcelDemy. Currently, he is working as a software developer. He is leading a team of six members to develop Microsoft Office Add-ins, extending Office applications to interact with office documents. Other assigned projects to his team include creating AI-based products and online conversion tools using the latest... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo