How do I convert the integer value \"45\" into the string value \"45\" in Excel VBA?
Try the CStr() function
Dim myVal as String;
Dim myNum as Integer;
myVal = "My number is:"
myVal = myVal & CStr(myNum);
Another way to do it is to splice two parsed sections of the numerical value together:
Cells(RowNum, ColumnNum).Value = Mid(varNumber,1,1) & Mid(varNumber,2,Len(varNumber))
I have found better success with this than CStr()
because CStr()
doesn't seem to convert decimal numbers that came from variants in my experience.
The shortest way without declaring the variable is with Type Hints :
s$ = 123 ' s = "123"
i% = "123" ' i = 123
This will not compile with Option Explicit
. The types will not be Variant
but String
and Integer
Sub NumToText(ByRef sRng As String, Optional ByVal WS As Worksheet)
'---Converting visible range form Numbers to Text
Dim Temp As Double
Dim vRng As Range
Dim Cel As Object
If WS Is Nothing Then Set WS = ActiveSheet
Set vRng = WS.Range(sRng).SpecialCells(xlCellTypeVisible)
For Each Cel In vRng
If Not IsEmpty(Cel.Value) And IsNumeric(Cel.Value) Then
Temp = Cel.Value
Cel.ClearContents
Cel.NumberFormat = "@"
Cel.Value = CStr(Temp)
End If
Next Cel
End Sub
Sub Macro1()
Call NumToText("A2:A100", ActiveSheet)
End Sub
Reffer: MrExcel.com – Convert numbers to text with VBA