How Do I Convert an Integer to a String in Excel VBA?

后端 未结 10 1754
遥遥无期
遥遥无期 2020-12-07 15:53

How do I convert the integer value \"45\" into the string value \"45\" in Excel VBA?

相关标签:
10条回答
  • 2020-12-07 16:43

    Try the CStr() function

    Dim myVal as String;
    Dim myNum as Integer;
    
    myVal = "My number is:"
    myVal = myVal & CStr(myNum);
    
    0 讨论(0)
  • 2020-12-07 16:44

    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.

    0 讨论(0)
  • 2020-12-07 16:48

    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

    0 讨论(0)
  • 2020-12-07 16:57

        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

    0 讨论(0)
提交回复
热议问题