如何在 Excel VBA 中将整数转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11595226/
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 Do I Convert an Integer to a String in Excel VBA?
提问by What'sUP
How do I convert the integervalue "45" into the stringvalue "45" in Excel VBA?
如何在 Excel VBA中将整数值“45”转换为字符串值“45”?
回答by Yosem
CStr(45)
is all you need (the Convert String function)
CStr(45)
就是你所需要的(转换字符串函数)
回答by Brian
Try the CStr() function
试试 CStr() 函数
Dim myVal as String;
Dim myNum as Integer;
myVal = "My number is:"
myVal = myVal & CStr(myNum);
回答by LimaNightHawk
Most times, you won't need to "convert"; VBA will do safe implicit type conversion for you, withoutthe use of converters like CStr
.
大多数情况下,您不需要“转换”;VBA 将为您进行安全的隐式类型转换,而无需使用 CStr
.
The below code works without any issues, because the variable is of Type String, and implicit type conversion is done for you automatically!
下面的代码没有任何问题,因为变量是字符串类型,隐式类型转换会自动为你完成!
Dim myVal As String
Dim myNum As Integer
myVal = "My number is: "
myVal = myVal & myNum
Result:
结果:
"My number is: 0"
“我的号码是:0”
You don't even have to get that fancy, this works too:
你甚至不必太花哨,这也有效:
Dim myString as String
myString = 77
"77"
“77”
The only time you WILL need to convert is when the variable Type is ambiguous(e.g., Type Variant, or a Cell's Value
(which isVariant)).
您将需要转换的唯一时间是当变量类型是不明确的(例如,类型变体或细胞的Value
(这是变))。
Even then, you won't have to use CStr
function if you're compounding with another String variable or constant. Like this:
即便如此,CStr
如果您与另一个 String 变量或常量复合,您也不必使用函数。像这样:
Sheet1.Range("A1").Value = "My favorite number is " & 7
"My favorite number is 7"
“我最喜欢的数字是 7”
So, really, the only rarecase is when you really want to store an integer value, into a variant or Cell value, when notalso compounding with another string (which is a pretty rare side case, I might add):
所以,真的,唯一罕见的情况是当你真的想将一个整数值存储到一个变体或 Cell 值中,而不是与另一个字符串复合时(这是一个非常罕见的副作用,我可能会补充):
Dim i as Integer
i = 7
Sheet1.Range("A1").Value = i
7
7
Dim i as Integer
i = 7
Sheet1.Range("A1").Value = CStr(i)
"7"
“7”
回答by Julian Kuchlbauer
In my case, the function CString was not found. But adding an empty string to the value works, too.
就我而言,没有找到函数 CString。但是向该值添加一个空字符串也有效。
Dim Test As Integer, Test2 As Variant
Test = 10
Test2 = Test & ""
//Test2 is now "10" not 10
回答by Slai
The shortest way without declaring the variable is with Type Hints:
不声明变量的最短方法是使用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
这不会与Option Explicit
. 类型不会是Variant
但是String
和Integer
回答by dolphus333
If the string you're pulling in happens to be a hex number such as E01, then Excel will translate it as 0 even if you use the CStr function, and even if you first deposit it in a String variable type. One way around the issue is to append ' to the beginning of the value.
如果您拉入的字符串恰好是一个十六进制数,例如 E01,那么即使您使用 CStr 函数,并且即使您首先将它存入 String 变量类型,Excel 也会将其转换为 0。解决此问题的一种方法是将 ' 附加到值的开头。
For example, when pulling values out of a Word table, and bringing them to Excel:
例如,当从 Word 表格中提取值并将它们导入 Excel 时:
strWr = "'" & WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
回答by Jonathan
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.
我发现这比CStr()
因为CStr()
在我的经验中似乎没有转换来自变体的十进制数更成功。
回答by user2648008
If you have a valid integer value and your requirement is to compare values, you can simply go ahead with the comparison as seen below.
如果您有一个有效的整数值并且您的要求是比较值,您可以简单地继续进行比较,如下所示。
Sub t()
Dim i As Integer
Dim s As String
' pass
i = 65
s = "65"
If i = s Then
MsgBox i
End If
' fail - Type Mismatch
i = 65
s = "A"
If i = s Then
MsgBox i
End If
End Sub
回答by Mahhdy
The accepted answer is good for smaller numbers, most importantly while you are taking data from excel sheets. as the bigger numbers will automatically converted to scientific numbers i.e. e+10.
So I think this will give you more general answer. I didn't check if it have any downfall or not.
接受的答案适用于较小的数字,最重要的是,当您从 Excel 表格中获取数据时。因为较大的数字会自动转换为科学数字,即 e+10。
所以我认为这会给你更一般的答案。我没有检查它是否有任何垮台。
CStr(CDbl(#yourNumber#))
this will work for e+ converted numbers! as the just CStr(7.7685099559e+11)
will be shown as "7.7685099559e+11" not as expected: "776850995590" So I rather to say my answer will be more generic result.
这将适用于 e+ 转换的数字!因为只是CStr(7.7685099559e+11)
将显示为“7.7685099559e+11”而不是预期:“776850995590”所以我宁愿说我的答案将是更通用的结果。
Regards, M
问候, M
回答by Gajendra Santosh
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