Excel VBA Set Cell Format to Text: A Complete Guide
In this article
To set a cell format to text using Excel VBA, use the .NumberFormat = "@" property. This tells Excel to treat every character entered into the cell as a literal string, preventing the software from automatically converting long numbers into scientific notation or removing leading zeros from IDs and zip codes.
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- The Developer tab enabled in the Excel Ribbon (Right-click any tab > Customize the Ribbon > Check "Developer").
- Basic access to the Visual Basic Editor (VBE), accessible via Alt + F11.
- A dataset that requires specific formatting, such as SKU numbers, phone numbers, or credit card digits.
Step-by-Step
- Open the Visual Basic Editor: Launch your Excel workbook and press
Alt + F11on your keyboard. This opens the VBE window where all your macros and modules are managed. - Insert a New Module: In the top menu, go to
Insertand selectModule. This creates a blank space where you can write your custom VBA procedure. - Define Your Subroutine: Start your code by typing
Sub SetFormatToText()and press Enter. This names your macro and tells Excel where the instructions begin. - Identify the Target Range: You must tell VBA which cells to format. You can target a single cell, a specific range, or an entire column. For example, to target column A, use
Columns("A:A"). To target a specific range, useRange("A1:B10"). - Apply the NumberFormat Property: Use the
.NumberFormatproperty assigned to the"@"symbol. The "@" is the internal Excel code for the Text format.
Example for a specific range:Range("A1:A100").NumberFormat = "@"
Example for the currently selected cells:Selection.NumberFormat = "@" - Add a Safety Check (Optional): If you are working with a dynamic sheet, it is better to reference the worksheet explicitly to avoid formatting the wrong sheet. Use
Sheets("Sheet1").Range("A:A").NumberFormat = "@". - Close and Run: Close the VBE window and return to Excel. Press
Alt + F8, selectSetFormatToTextfrom the list, and clickRun.
Common Mistakes
One of the most frequent errors is applying the text format after the data has already been entered. If you have a long number (like a 16-digit credit card number) and Excel has already converted it to scientific notation (e.g., 1.23E+15), changing the format to "@" via VBA will not magically restore the lost digits. It will simply format the scientific notation string as text. To avoid this, you must apply the .NumberFormat = "@" command before the data is written to the cell or imported.
Another common mistake is confusing .NumberFormat with .Value. Setting the format changes how Excel interprets future input; it does not change the underlying data type of a value that is already stored as a number. If you need to force an existing number to become text, you may need to append an empty string to the value, such as Range("A1").Value = Range("A1").Value & "", after setting the format to text.
Users also frequently forget to specify the worksheet. Using Range("A1") without a worksheet qualifier tells VBA to apply the format to whatever sheet happens to be active. In a complex workbook with multiple tabs, this can lead to accidental formatting of the wrong data, which can be difficult to undo if the workbook is saved.
Tips
When dealing with massive datasets, avoid using Selection. Selecting cells in VBA slows down the execution significantly because Excel has to visually update the screen. Instead, reference the range directly. For example, use Worksheets("Data").Columns("C").NumberFormat = "@" rather than selecting column C and then formatting it.
If you are importing CSV data via VBA, the best practice is to format the entire destination column as text before the import process begins. This ensures that leading zeros (common in zip codes or employee IDs) are preserved and not truncated by Excel's default "General" format.
For those who want to ensure that data entered by users is always treated as text, consider using a Worksheet_Change event. This allows you to automatically apply the text format to any cell in a specific column the moment a user clicks into it or enters data, ensuring consistency across a shared team spreadsheet.
Troubleshooting
The format changed, but the numbers still look like scientific notation: This happens because the cell value was already converted. To fix this, you must re-enter the data or use a formula to convert the number back to a string. Try double-clicking the cell and pressing Enter; if the format was set to "@" correctly, it should now display as text.
The VBA code throws a "Run-time error 1004": This usually occurs if the worksheet is protected. VBA cannot change the formatting of a locked cell on a protected sheet. To resolve this, add ActiveSheet.Unprotect "YourPassword" at the start of your macro and ActiveSheet.Protect "YourPassword" at the end.
The "@" symbol is appearing literally in the cell: This occurs if you accidentally set the .Value of the cell to "@" instead of the .NumberFormat. Check your code to ensure you are using .NumberFormat = "@" and not .Value = "@".
The macro runs but nothing happens: Ensure you are targeting the correct sheet. If your code says Range("A1") but you are looking at "Sheet2" while the macro is acting on "Sheet1", you won't see the changes. Always use explicit references like ThisWorkbook.Sheets("SheetName").Range("A1").
Frequently Asked Questions
Can I set the format to text for the entire workbook?
There is no single command to format every cell in a workbook as text, as this would consume unnecessary memory. Instead, you can loop through all worksheets using a For Each ws In ThisWorkbook.Worksheets loop and apply the .NumberFormat = "@" to the UsedRange of each sheet.
Does setting a cell to text remove the ability to do math?
Yes, if a cell is formatted as text, Excel treats it as a string. While some basic formulas might still work because Excel tries to "coerce" the text back into a number, it is generally not recommended for cells intended for calculations. Use text format only for identifiers, not for currency or quantities.
What is the difference between .NumberFormat = "@" and adding an apostrophe?
Adding an apostrophe (') at the start of a cell entry is a manual way to tell Excel to treat the entry as text. Using .NumberFormat = "@" via VBA is the programmatic equivalent. Both achieve the same result, but the VBA method is scalable for thousands of cells.
Will this prevent Excel from removing leading zeros?
Yes. This is the primary reason for using the text format. When a cell is set to "@", a value like "00123" will remain exactly as entered, whereas the "General" format would automatically convert it to "123".
Can I change the format back to General using VBA?
Yes. To revert a cell or range to the default format, use Range("A1").NumberFormat = "General". This will allow Excel to once again automatically determine if the input is a number, date, or text.
Does this work on Excel for Mac?
Yes, the .NumberFormat = "@" property is standard across both Windows and Mac versions of Excel VBA, although the keyboard shortcuts to open the VBE may differ slightly.