Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Converting Numbers to Strings.

# Converting Numbers to Strings

Written by Allen Wyatt (last updated August 21, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365

You already know that you can use variables in your macros, and that there are two very basic types of variables: string variables (containing characters) and numeric variables (containing numeric values). You can quickly and easily convert a number into a string in your macros. This is the done with the Str() function. The way you use this function is as follows:

A = Str(B)

In this syntax, if B is equal to 5, then when completed, A will be " 5"; if B is -4, then A would be "-4". Notice the leading space when converting positive numbers. This may not provide satisfactory results for some subroutines. Instead, you should create a function that returns a stripped-down version of the string. The following function does just that:

Function ToNum(X as Variant) as String
ToNum = Trim(Str(X))
End Function

The reason that the value passed to the VBA function (X) is defined as a Variant is that you can then pass any type of numeric value.

An alternative approach is to use the following variation of the function:

Function ToNum(X as Variant) as String
ToNum = CStr(X)
End Function

Either approach will work just fine.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9749) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Converting Numbers to Strings.

##### Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

##### MORE FROM ALLEN

Removing Line Numbering

If you've added line numbers to your document, at some point you may want them turned off. Here's how to disable line ...

Discover More

When applying trigonometry to the values in a worksheet, you may need to convert radians to degrees. This is done by ...

Discover More

Unwanted Data Changes

The AutoCorrect feature in Excel can change some of your data in ways you don't like. If you discover this is happening, ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

##### More ExcelTips (ribbon)

Page Numbers in VBA

When you print a larger worksheet, Excel breaks the printout across several pages. You may want to know, before you ...

Discover More

Trouble Recording Paste Special Formula

Sometimes, when you upgrade to a new version of Excel, you could run into a problem recording macros that you had no ...

Discover More

Stopping Excel from Deleting Macros from a Workbook

When working with very large workbooks, it is possible for Excel to behave erratically. This tip looks at ways you can ...

Discover More
##### Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. Youâ€™ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is eight minus 0?

2019-08-03 06:00:59

Alex B

Using "Str" in the macro means it will error out if the cell reference you pass it contains a non-numeric value. eg aaa in the cell will result in #VALUE.
It is also unnecessary both the following options work fine without it.

Function ToNum(X As Variant) As String
ToNum = Trim(X)
End Function

And even just relying on defining ToNum as a string is enough to do the job
Function ToNum(X As Variant) As String
ToNum = X
End Function

##### This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.