如何将数字转换为字符串 - VBA

时间:2017-07-07 07:42:36

标签: excel vba excel-vba

我试图将数值转换为strin ex。将10转换为" 10"

我试过

    With Range("B2:B" & NewResizeRange)
      .Value = Range("B2:B" & NewResizeRange).Value
      .NumberFormat = "0" ' Or .NumberFormat = "@"
   End With

但我们没有正确转换。有什么想法吗?

4 个答案:

答案 0 :(得分:1)

With Range("A1")
    .NumberFormat = "@"
    .Value = .Value
End With

这将使Number stored as text的绿色三角形显示出来并表示您的单元格确实包含文本值。但是,当使用这样的单元格作为公式的参数进行计算时,Excel将自行进行一些转换。

如果您不想使用NumberFormat,您也可以这样做:

Range("A1").value = "=""" & Range("A1").value & """" - 这会创建一个公式(如果值为10,则会生成公式="10"),强制值计算为字符串。

答案 1 :(得分:0)

首先尝试选择范围。所以:

   With Range("B2:B" & NewResizeRange)
      .Value = Range("B2:B" & NewResizeRange).Value
      .Select
      .Selection.NumberFormat = "@"
   End With

希望这有帮助。

答案 2 :(得分:0)

在Excel VBA中将整数转换为字符串,以印度卢比为印度货币

通过创建模块将此代码粘贴到宏中

函数名称为ConvertIndianCurrency

   Function ConvertIndianCurrency(ByVal MyNumber)
Dim Temp
Dim Count, DecimalPlace
ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Lakh "
Place(4) = " Crore "
' convert MyNumber to a string, trimming extra spaces.
MyNumber = Trim(Str(MyNumber))
' Find decimal place.
DecimalPlace = InStr(MyNumber, ".")
' If we find decimal place...
If DecimalPlace > 0 Then
    ConvertIndianCurrency = "Please Remove Decimal Places in the Number..."
    Exit Function
End If
Count = 1
If MyNumber <> "" Then
    ' convert last 3 digits of MyNumber to Indian Rupees.
    Temp = ConvertHundreds(Right(MyNumber, 3))
    If Temp <> "" Then
        Rupees = Temp
    End If
    If Len(MyNumber) > 3 Then
        ' Remove last 3 digits from MyNumber.
        MyNumber = Left(MyNumber, Len(MyNumber) - 3)
    Else
        MyNumber = ""
    End If
End If
Do While MyNumber <> ""
    Count = Count + 1
    Temp = ConvertTwoDigits(Right(MyNumber, 2))
    If Temp <> "" Then
        Rupees = Temp & Place(Count) & Rupees
    End If
    If Len(MyNumber) > 2 Then
        ' Remove last 2 converted digits from MyNumber.
        MyNumber = Left(MyNumber, Len(MyNumber) - 2)
    Else
        MyNumber = ""
    End If
Loop
' Clean up Rupees.
Select Case Rupees
    Case ""
        Rupees = "Rupees Nil"
    Case "One"
        Rupees = "Rupee One Only"
    Case Else
        Rupees = "Rupees " & Rupees & " Only"
End Select
ConvertIndianCurrency = Rupees
End Function

Private Function ConvertHundreds(ByVal MyNumber)
Dim Result As String
' Exit if there is nothing to convert.
If Val(MyNumber) = 0 Then
    Exit Function
End If
' Append leading zeros to number.
MyNumber = Right("000" & MyNumber, 3)
' Do we have a hundreds place digit to convert?
If Left(MyNumber, 1) <> "0" Then
    Result = ConvertDigit(Left(MyNumber, 1)) & " Hundred "
End If
' Do we have a tens place to convert?
If Mid(MyNumber, 2) <> "0" Then
    Result = Result & ConvertTens(Mid(MyNumber, 2))
Else
    ' If not, then convert the ones place digit.
    Result = Result & ConvertDigit(Mid(MyNumber, 3))
End If
ConvertHundreds = Trim(Result)
End Function

Private Function ConvertTwoDigits(ByVal MyNumber)
Dim Result As String
' Exit if there is nothing to convert.
If Val(MyNumber) = 0 Then
    Exit Function
End If
' Append leading zeros to number.
MyNumber = Right("00" & MyNumber, 2)
' Do we have a tens place to convert?
If Left(MyNumber, 1) <> "0" Then
    Result = ConvertTens(Mid(MyNumber, 1))
Else
    Result = Result & ConvertDigit(Right(MyNumber, 1))
End If
ConvertTwoDigits = Trim(Result)
End Function

Private Function ConvertTens(ByVal MyTens)
Dim Result As String
' Is value between 10 and 19?
If Val(Left(MyTens, 1)) = 1 Then
    Select Case Val(MyTens)
        Case 10: Result = "Ten"
        Case 11: Result = "Eleven"
        Case 12: Result = "Twelve"
        Case 13: Result = "Thirteen"
        Case 14: Result = "Fourteen"
        Case 15: Result = "Fifteen"
        Case 16: Result = "Sixteen"
        Case 17: Result = "Seventeen"
        Case 18: Result = "Eighteen"
        Case 19: Result = "Nineteen"
        Case Else
    End Select
Else
    ' ... otherwise its between 20 and 99.
    Select Case Val(Left(MyTens, 1))
        Case 2: Result = "Twenty "
        Case 3: Result = "Thirty "
        Case 4: Result = "Forty "
        Case 5: Result = "Fifty "
        Case 6: Result = "Sixty "
        Case 7: Result = "Seventy "
        Case 8: Result = "Eighty "
        Case 9: Result = "Ninety "
        Case Else
    End Select
    ' convert ones place digit.
    Result = Result & ConvertDigit(Right(MyTens, 1))
End If
ConvertTens = Result
End Function
Private Function ConvertDigit(ByVal MyDigit)
Select Case Val(MyDigit)
    Case 1: ConvertDigit = "One"
    Case 2: ConvertDigit = "Two"
    Case 3: ConvertDigit = "Three"
    Case 4: ConvertDigit = "Four"
    Case 5: ConvertDigit = "Five"
    Case 6: ConvertDigit = "Six"
    Case 7: ConvertDigit = "Seven"
    Case 8: ConvertDigit = "Eight"
    Case 9: ConvertDigit = "Nine"
    Case Else: ConvertDigit = ""
End Select
End Function

答案 3 :(得分:0)

Dim XN As Integer
Dim XS As String

XN = 10
XS = Trim(Str(XN))  'now XS = "10"