excel如何自动提取不规则文字串的数字
问题1:
Sub 取数字()
Set lk = CreateObject("vbscript.regexp")
lk.Global = True
lk.Pattern = "[0-9]{1,}"
For Each Ln In Range("a1", Cells(Rows.Count, "a").End(xlUp).Address)
For Each lp In lk.Execute(Ln)
Cells(Ln.Row, "c") = lp.Value
Next
Next
End Sub
问题2:
Sub 取数字()
n = InputBox("请输入需要提取第多少次出现的数字字符串", "提示", vbOKOnly)
If VBA.IsNumeric(n) = False Then
MsgBox "输入的不是数字", vbInformation, "错误提示"
Exit Sub
End If
Set lk = CreateObject("vbscript.regexp")
lk.Global = True
lk.Pattern = "[0-9]{1,}"
For Each Ln In Range("a1", Cells(Rows.Count, "a").End(xlUp).Address)
For Each lp In lk.Execute(Ln)
k = k + 1
If k = CInt(n) Then
Cells(Ln.Row, "c") = lp.Value
Exit Sub
End If
Next
Next
End Sub