Excel VBA Range Text: Read and Write Cell Text
In this article
You read or write the text of a cell in Excel VBA by going through the Range object and its .Value, .Value2, .Text or .Formula properties. The shortest working version is Range("A1").Value = "Hello" to write and MsgBox Range("A1").Value to read. The rest of this guide explains which property to pick, how to handle multi-cell ranges, and the traps that make text come back wrong.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- A workbook saved as .xlsm if you want to keep your code.
- Basic familiarity with opening the VBA editor (Alt+F11 on Windows, Option+F11 on Mac) and inserting a module (Insert > Module).
Menu labels can differ slightly by Excel version, platform and region, so if a path looks different, look for the nearest equivalent.
Step-by-Step
- Open the VBA editor and add a module. Press Alt+F11 (Windows) or Option+F11 (Mac), then choose Insert > Module. All the code below goes in that module.
- Write text to a single cell. Use the
.Valueproperty:
This puts a text string in A1. Excel decides the data type from what you assign, so a quoted string stays text even if it looks like a number.Sub WriteText() Range("A1").Value = "Hello world" End Sub - Read text from a single cell. Assign the cell to a variable or show it:
Declaring the variable asSub ReadText() Dim s As String s = Range("A1").Value MsgBox s End SubStringforces VBA to convert whatever is in the cell to text. - Know when to use .Text instead of .Value.
.Textreturns exactly what the cell displays, including formatting such as currency symbols, thousands separators and the result of a number format..Valuereturns the underlying value. If A1 holds 1234.5 formatted as $1,234.50,.Valuegives 1234.5 and.Textgives "$1,234.50". Use.Textwhen you need the on-screen string; use.Valuewhen you need the real data. - Handle a multi-cell range. Reading
.Valuefrom a range larger than one cell returns a two-dimensional array, not a single string:
Note the array is 1-based, soSub ReadBlock() Dim v As Variant v = Range("A1:B3").Value MsgBox v(1, 1) ' first row, first column End Subv(1,1)is the top-left cell. Looping withFor r = 1 To 3andFor c = 1 To 2lets you process each cell. - Join text from several cells. If you want one string from a block, loop and concatenate, or use
Joinon a one-dimensional array. For a single row or column you can also useWorksheetFunction.TextJoinif your Excel version supports it; otherwise build the string manually to stay compatible. - Write text to a multi-cell range. Assign a single string and Excel fills every cell in the range with it. To write different values, assign an array of the same shape:
Shapes must match or you get a run-time error.Range("A1:B2").Value = Array(Array("a", "b"), Array("c", "d")) - Use .Formula when the cell contains a formula.
.Valueon a formula cell returns the calculated result;.Formulareturns the formula text itself, for example"=A1&\" \"&B1". Use.Formulawhen you need to inspect or copy the formula string. - Trim and clean the text. Cells often carry trailing spaces or line breaks. Wrap reads in
Trim()to remove leading and trailing spaces, andReplace(s, Chr(10), " ")to flatten line breaks. This prevents comparisons that look identical but fail. - Test with a message box, then remove it. Run your sub with F5, confirm the output, and delete any
MsgBoxlines before using the macro in production.
Common Mistakes
- Using .Text on a narrow column. If the column is too narrow,
.Textreturns"####"instead of the real value. Widen the column or use.Value. - Assuming .Value is always a string. A cell containing 42 returns a number. Assigning to a
Stringvariable converts it, but comparing it to"42"without conversion can behave unexpectedly. UseCStr()when you need a guaranteed string. - Forgetting the array shape. Reading a 2D range into a 1D variable causes a type mismatch. Always use a
Variantfor multi-cell reads. - Writing text that starts with = or +. Excel may treat it as a formula. Prefix with an apostrophe or set
.NumberFormat = "@"before writing. - Leaving the sheet unqualified.
Range("A1")refers to the active sheet. If another sheet is active when the macro runs, you write to the wrong place. Qualify withWorksheets("Sheet1").Range("A1").
Tips
- Use
.Value2instead of.Valuewhen you want the raw number without date or currency coercion;.Value2returns dates as serial numbers. - Set
Application.ScreenUpdating = Falseat the start and back toTrueat the end when writing to many cells; it speeds up large loops noticeably. - Read a whole range into an array once, process it in memory, then write it back in one assignment. This is far faster than touching each cell individually.
- Use
Range("A1").Value = vbNullStringto clear a cell's contents without disturbing its formatting.
Troubleshooting
If the macro does nothing, check that macros are enabled (File > Options > Trust Center > Trust Center Settings > Macro Settings) and that the code is in a standard module, not a sheet module, unless you intended it to run on an event.
If you get "Run-time error 1004", the most common causes are an unqualified range on the wrong sheet, a misspelled sheet name, or writing an array of the wrong shape. Step through the code with F8 to see exactly which line fails.
If text comes back as a number or a date, the cell's format is coercing it. Read with .Text or convert with CStr(). If the value is a date serial and you want the displayed date, use .Text or format the string with Format().
If .Text returns "####", widen the column or switch to .Value. If the macro is slow on large ranges, switch to array reads and disable screen updating as described in the Tips section.
Frequently Asked Questions
What is the difference between Range.Value and Range.Text?
.Value returns the underlying value (a number, date or string). .Text returns the string as displayed, including number formatting. Use .Value for data processing and .Text when you need the visible string.
How do I read text from a cell into a string variable?
Declare Dim s As String and assign s = Range("A1").Value. VBA converts the value to text automatically. Use CStr() if you want the conversion to be explicit.
Why does my multi-cell read return an error?
Reading .Value from more than one cell returns a 2D array, so the receiving variable must be a Variant. Index it with v(row, column), starting at 1.
Can I write text that looks like a formula without Excel evaluating it?
Yes. Set the cell's .NumberFormat = "@" first, or prefix the string with an apostrophe. Either keeps the text literal.
Does Range.Text work on a hidden or very narrow column?
It can return "####" when the column is too narrow to display the value. Widen the column or use .Value instead.
How do I clear text from a range?
Use Range("A1:A10").ClearContents to remove values while keeping formatting, or assign vbNullString to each cell.