如何将数字转换为字符串 - VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44965245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert number to string - VBA
提问by Jordan
I'm trying to conver a number value to a strin ex. convert 10 to "10"
我正在尝试将数值转换为 strin ex。将 10 转换为“10”
I tried
我试过
With Range("B2:B" & NewResizeRange)
.Value = Range("B2:B" & NewResizeRange).Value
.NumberFormat = "0" ' Or .NumberFormat = "@"
End With
But it us not converting correctly. Any ideas?
但我们没有正确转换。有任何想法吗?
回答by Rik Sportel
With Range("A1")
.NumberFormat = "@"
.Value = .Value
End With
This will make the green triangle for Number stored as text
show up and mean that your cell will indeed hold a text value. However, when doing calculations using such a cell as a parameter for the formula, Excel will do some conversion by itself.
这将使绿色三角形Number stored as text
显示出来,并意味着您的单元格确实会保存一个文本值。但是,在使用这样的单元格作为公式参数进行计算时,Excel 会自行进行一些转换。
If you do not want to use NumberFormat
, you could also do:
如果你不想使用NumberFormat
,你也可以这样做:
Range("A1").value = "=""" & Range("A1").value & """"
- This creates a formula (If the value was 10, it'll make a formula ="10"
) that forces the value to evaluate to a string.
Range("A1").value = "=""" & Range("A1").value & """"
- 这将创建一个公式(如果值为 10,它将创建一个公式="10"
),该公式强制该值计算为字符串。
回答by S FAIZULLAH BASHA
Convert Integer into String in Excel VBA for Indian Currency in Rupees
在 Excel VBA 中将整数转换为字符串,以卢比表示印度货币
Paste this code in macro by creating a module
通过创建模块将此代码粘贴到宏中
Function name is ConvertIndianCurrency
函数名称是 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
回答by Matu
Dim XN As Integer
Dim XS As String
XN = 10
XS = Trim(Str(XN)) 'now XS = "10"
回答by Roosz0rd
Try selecting your range first. So:
尝试先选择您的范围。所以:
With Range("B2:B" & NewResizeRange)
.Value = Range("B2:B" & NewResizeRange).Value
.Select
.Selection.NumberFormat = "@"
End With
Hope this helps.
希望这可以帮助。