古詩詞大全網 - 成語經典 - excel如何自動提取不規則文字串的數字

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