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.
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:
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.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
If you have a series of values in a column, you might have a need to separate the values into even values and odd values. ...
Discover MoreGrab some info from a source other than Excel, and you may find the need to delete a certain pattern of rows from a ...
Discover MoreMacros are stored as part of a workbook so that they are always available when you have the workbook open. If you want to ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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 © 2024 Sharon Parq Associates, Inc.
Comments