Written by Allen Wyatt (last updated September 6, 2025)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, 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(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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9749) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, 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.
Best-Selling VBA Tutorial for Beginners Take your Excel knowledge to the next level. With a little background in VBA programming, you can go well beyond basic spreadsheets and functions. Use macros to reduce errors, save time, and integrate with other Microsoft applications. Fully updated for the latest version of Office 365. Check out Microsoft 365 Excel VBA Programming For Dummies today!
Want to run a macro when you first select a worksheet? You can do so by using one of the event handlers built into Excel, ...
Discover MoreNeed to know the character code used for a particular character? In a macro you can use the Asc function to determine the ...
Discover MoreWhen you enter information into a workbook, Excel automatically recalculates every worksheet in every open workbook on ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2025-09-07 10:32:34
J. Woolley
IMHO, creating a function like ToNum that simply returns the result of another function is dumb.
2025-09-06 10:46:04
J. Woolley
As noted later in the Tip, A = Str(B) is not the correct way to convert a numeric data type into a String data type; use A = CStr(B) instead. CStr(...) converts any data type into a String data type. Str(...) returns a string "representation" of a number.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments