[英]Excel VBA Macro - String to Decimal
I'm trying to convert a cell value containing a coordinate to an integer. 我正在尝试将包含坐标的单元格值转换为整数。 The string value is "X-324.45", but would like to be able to use this number for an if then loop later.
字符串值为“ X-324.45”,但希望以后可以在if then循环中使用此数字。
Msgbox1 shows the string as X-324.45 Msgbox2 shows the string as -324.45 Msgbox3 shows a blank MsgBox Msgbox1显示字符串为X-324.45 Msgbox2显示字符串为-324.45 Msgbox3显示空白MsgBox
What am I missing here? 我在这里想念什么? I would like -324.45 to be a variable I can use for math operations.
我希望-324.45是可以用于数学运算的变量。
Thanks, 谢谢,
Dim YVAL1 As String
Dim YVAL2 As String
Dim YVAL3 As Double
YVAL1 = ActiveCell.Offset(0, -4).Value
YVAL2 = Mid(YVAL1, 1, 10)
YVAL3 = CDbl(YVAL2)
MsgBox ("Value1: " + YVAL1)
MsgBox ("Value2: " + YVAL2)
MsgBox ("Value3: " + YVAL3)
Mid(YVAL1, 1, 10)
does not remove anything - to read from the 2nd character Mid(YVAL1, 2, 10)
. Mid(YVAL1, 1, 10)
不会删除任何内容-从第二个字符Mid(YVAL1, 2, 10)
读取。
MsgBox ("Value3: " + YVAL3)
fails because it attempts the arithmetic addition of a non-convertible string to a double - always use &
to concatenate: MsgBox ("Value3: " + YVAL3)
失败,因为它尝试将不可转换字符串算术加到double中-始终使用&
进行串联:
MsgBox ("Value3: " & YVAL3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.