如何提取电话号码并拆分到Google表格中的列?

时间:2019-05-21 20:51:04

标签: regex vba google-sheets array-formulas google-sheets-formula

我正尝试提取此格式(123)456-7890的电话号码,并将每个电话号码放在格式为1234567890的单独列中,且不包含空格,破折号或括号。

我能够使用另一个StackOverflow问题中的以下VBA代码在Excel中实现此功能,但无法使其在Google表格上运行


Sub ewqre()
    Dim str As String, n As Long, rw As Long
    Dim rgx As Object, cmat As Object, ws As Worksheet

    Set rgx = CreateObject("VBScript.RegExp")
    Set ws = Worksheets("Sheet4")

    With rgx
        .Global = True
        .MultiLine = True
        'phone number pattern is: ###-###-####
        .Pattern = "/^(\d{3})(\d{3})(\d{4})$/"
        For rw = 2 To ws.Cells(Rows.Count, "A").End(xlUp).Row
            str = ws.Cells(rw, "A").Value2
            If .Test(str) Then
                Set cmat = .Execute(str)
                'populate the worksheet with the matches
                For n = 0 To cmat.Count - 1
                    ws.Cells(rw, Columns.Count).End(xlToLeft).Offset(0, 1) = cmat.Item(n)
                Next n
            End If
        Next rw
    End With

    Set rgx = Nothing: Set ws = Nothing

End Sub

1 个答案:

答案 0 :(得分:2)

=ARRAYFORMULA(IFERROR(REGEXEXTRACT(TO_TEXT(SPLIT(
 REGEXREPLACE(A2:A, "\(|\)|\-| ", ""), CHAR(10))), "\d+")))


或:

=ARRAYFORMULA(REGEXREPLACE(REGEXEXTRACT(SPLIT(A2, CHAR(10)), 
 "\((.*)\(.T"),"\)|\s|\-", ""))

0

真正的数组公式为:

=ARRAYFORMULA(IFERROR(REGEXREPLACE(REGEXEXTRACT(SPLIT(A2:A, CHAR(10)), 
 "\((.*)\(.T"),"\)|\s|\-", "")))