2

我有一个非常复杂的 excel 任务,我完全被难住了。我正在尝试从 PDF 的页面抓取中提取一个 7 位整数,问题是它可以被文本和两侧不同长度的其他整数包围,唯一的唯一标识符是它将是 7 个字符长度。这是该列的外观:

A
3411491 159 美元
灰色5003195双 1,399 美元
库存加海军5020081

如何使用公式将斜体 7 位数字输出并输入到他们自己的单元格中?

4

2 回答 2

1

您将需要使用正则表达式。

您需要参考(工具 > 参考)“Microsoft VBScript 正则表达式 5.5”

试试下面的代码,它应该给你你想要的,匹配任何 7 位数字的序列。如果可能有多个 7 位数字序列或多个 7 位数字序列,则需要对其进行修改

Sub FindNumber()
' Reference: Microsoft VBScript Regular Expressions 5.5

Dim RegEx As Object ' VBScript_RegExp_55.regexp
Dim MatchCol As MatchCollection
Set RegEx = New RegExp


With RegEx
    .Pattern = "(.*)([0-9]{7})(.*)"
    .IgnoreCase = True
    .Global = True
End With

For i = 1 To 3 Step 1

    If RegEx.Test(ActiveSheet.Cells(i, 1).Value) Then


        Set MatchCol = RegEx.Execute(ActiveSheet.Cells(i, 1).Value)

        ActiveSheet.Cells(i, 2).Value = MatchCol(0).SubMatches(1)

    End If

Next i


End Sub
于 2013-07-03T17:16:24.603 回答
0

Press Alt+F11, copy the code into an empty module, check Microsoft VBScript Regular Expressions 5.5 option at Tools/References then use this as a User Defined Function in the next column.

Function ExtractNum(c As String) As String
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "(\b[\d]{7}\b)|.+?"
        If .Test(c) Then ExtractNum = Application.Trim(.Replace(c, "$1 "))
    End With
End Function
于 2013-07-04T16:11:27.677 回答