我想从字符串中提取单个数字。所以:
x = " 99 1.2 99.25 "
我想获得三个单独的数字:99
,1.2
和99.25
。
这是我目前的代码。它提取了第一个出现的数字,但我不知道如何使用循环来获取三个单独的数字。
Sub ExtractNumber()
Dim rng As Range
Dim TestChar As String
Dim IsNumber As Boolean
Dim i, StartChar, LastChar, NumChars As Integer
For Each rng In Selection
IsNumber = False
i = 1
Do While IsNumber = False And i <= Len(rng)
TestChar = Mid(rng, i, 1)
If IsNumeric(TestChar) = True Then
StartChar = i
IsNumber = True
End If
i = i + 1
Loop
IsNumber = False
Do While IsNumber = False And i <= Len(rng)
TestChar = Mid(rng, i, 1)
If IsNumeric(TestChar) = False Or i = Len(rng) Then
If i = Len(rng) Then
LastChar = i
Else
LastChar = i - 1
End If
IsNumber = True
End If
i = i + 1
Loop
NumChars = LastChar - StartChar + 1
rng.Offset(0, 1).Value = Mid(rng, StartChar, NumChars)
Next rng
End Sub
我之前的尝试(输入存储在单元格A6中):
Dim x, y, z As String
x = Range("A6")
y = Len(x)
For i = 1 To Len(x)
If IsNumeric(Mid(x, i, 1)) Then
z = z & Mid(x, i, 1)
End If
Next i
MsgBox z
答案 0 :(得分:1)
如果速度不是问题(如果任务不是密集的,等等),那么你可以使用这个
Public Sub splitme()
Dim a As Variant
Dim x As String
Dim i, j As Integer
Dim b() As Double
x = "1.2 9.0 0.8"
a = Split(x, " ")
j = 0
ReDim b(100)
For i = 0 To UBound(a)
If (a(i) <> "") Then
b(j) = CDbl(a(i))
j = j + 1
End If
Next i
ReDim Preserve b(j - 1)
End Sub
需要为b(100)
添加错误检查,以满足您的特定需求,并使用CDbl
。
如果要将其用作循环的一部分,或者用于大x
- 或两者兼而有之,请考虑其他选项,如RegEx(上一个答案) - 作为重复调用{{通常最好避免使用1}}。
答案 1 :(得分:0)
为什么不尝试使用正则表达式,而不是编写自己的代码来提取数字? This website有很多有关正则表达式的精彩信息和教程。起初可能有些令人困惑,但一旦你掌握了它,它就是解决这类问题的一个非常强大的工具。
以下是使用正则表达式对象后提取信息的示例。
Public Sub ExtractNumbers()
'Regular Expression Objects
Dim objRegEx As Object
Dim objMatches As Object
Dim Match As Object
'String variable for source string
Dim strSource As String
'Iteration variable
Dim i As Integer
'Create Regular Expression Object
Set objRegEx = CreateObject("VBScript.RegExp")
'Set objRegEx properties
objRegEx.Global = True '<~~ We want to find all matches
objRegEx.MultiLine = True '<~~ Allow line breaks in source string
objRegEx.IgnoreCase = False '<~~ Not strictly necessary for this example
'Below pattern matches an integer or decimal number 'word' within a string
' \b matches the start of the word
' [+-]? optionally matches a + or - symbol
' [0-9]+ matches one or more digits in sequence
' (\.[0-9]+)? optionally matches a period/decimal point followed by one or more digits
' \b matches the end of the word
objRegEx.Pattern = "\b[+-]?[0-9]+(\.[0-9]+)?\b"
'Example String
strSource = "x= 99 10.1 20.6 Aardvark"
'Ensure that at least one match exists
If objRegEx.Test(strSource) Then
'Capture all matches in objMatches
Set objMatches = objRegEx.Execute(strSource)
'TODO: Do what you want to do with them
'In this example I'm just printing them to the Immediate Window
'Print using Match object and For..Each
For Each Match In objMatches
Debug.Print Match.Value
Next Match
'Print using numeric iteration (objMatches.Items is a 0-based collection)
For i = 0 To (objMatches.Count - 1)
Debug.Print objMatches.Item(i)
Next i
End If
End Sub
此示例中显示的两个打印变体都会将以下输出打印到立即窗口
99
10.1
20.6