I wanted to know a way to reference part of a value in a cell in Excel in VBA. For instance if in cell A32 I have stored 4200 cc. I would like to reference only the 4200 for calculations in a macro in VBA. So like one can reference strings or lists in python with indices, I'd like to be able to do the same. Thank you!
我想知道在VBA中引用Excel中单元格中某部分值的方法。例如,如果在单元格A32中我存储了4200 cc。我想在VBA的宏中仅引用4200进行计算。因此,就像人们可以在带有索引的python中引用字符串或列表一样,我希望能够做同样的事情。谢谢!
3 个解决方案
#1
2
Something like this(?):
这样的东西(?):
Dim tmpArr
tmpArr = Split(Range("A32"), " ")
'any calculation in vba in this way:
Range("B32") = tmpArr(0) / 100
'results with 42 in cell B32
EDIT If there is any doubt about recognition of number for first part of the split results you could make additional conversion in this way:
编辑如果对拆分结果的第一部分的数字识别有任何疑问,您可以通过以下方式进行额外转换:
'conversion to Double type
Range("B32") = CDbl(tmpArr(0))/100
#2
0
You may create a function to split the content of the cell and then handle the parts you want separately.
您可以创建一个函数来拆分单元格的内容,然后单独处理所需的部分。
Dim content As Variant
content = Split(Range("A32").Value, " ")
#3
0
Get the cell value as a string s and return the numeric part?
获取单元格值作为字符串s并返回数字部分?
Function GetNumber(s As String)
Dim j As Long
While Not IsNumeric(Left(s, 1))
If Len(s) <= 1 Then
Exit Function
Else
s = Mid(s, 2)
End If
Wend
GetNumber = Val(s)
End Function
#1
2
Something like this(?):
这样的东西(?):
Dim tmpArr
tmpArr = Split(Range("A32"), " ")
'any calculation in vba in this way:
Range("B32") = tmpArr(0) / 100
'results with 42 in cell B32
EDIT If there is any doubt about recognition of number for first part of the split results you could make additional conversion in this way:
编辑如果对拆分结果的第一部分的数字识别有任何疑问,您可以通过以下方式进行额外转换:
'conversion to Double type
Range("B32") = CDbl(tmpArr(0))/100
#2
0
You may create a function to split the content of the cell and then handle the parts you want separately.
您可以创建一个函数来拆分单元格的内容,然后单独处理所需的部分。
Dim content As Variant
content = Split(Range("A32").Value, " ")
#3
0
Get the cell value as a string s and return the numeric part?
获取单元格值作为字符串s并返回数字部分?
Function GetNumber(s As String)
Dim j As Long
While Not IsNumeric(Left(s, 1))
If Len(s) <= 1 Then
Exit Function
Else
s = Mid(s, 2)
End If
Wend
GetNumber = Val(s)
End Function