excel中使用正则表达式提取单元格中匹配的字符

 时间:2026-02-13 09:33:48

1、新建一个EXCEL表格

excel中使用正则表达式提取单元格中匹配的字符

2、按ALT+F11打开VBA的代码窗口

excel中使用正则表达式提取单元格中匹配的字符

3、鼠标右键点击项目,选择“插入”->“模块”

excel中使用正则表达式提取单元格中匹配的字符

4、在新出的窗口中输入以下代码:

Function RegexMatch(cellValue As String, pattern As String)

    Dim regex As Object

    Dim matches As Object

    Dim match As Object

    Dim result As String

    

    Set regex = CreateObject("VBScript.RegExp")

    regex.Global = True

    regex.Pattern = pattern

    

    If regex.test(cellValue) Then

        Set matches = regex.Execute(cellValue)

        For Each match In matches

            result = result & match.Value & " "

        Next match

        RegexMatch = Trim(result)

    Else

        RegexMatch = "No match found"

    End If

End Function

excel中使用正则表达式提取单元格中匹配的字符

5、并将文件保存为 excel启用宏的工作薄

excel中使用正则表达式提取单元格中匹配的字符

6、最后可以在单元格中输入=RegexMatch(A3,"\d+"),可以获取对应的内容

excel中使用正则表达式提取单元格中匹配的字符

  • 在Excel中如何利用正则表达式来提取数值
  • excel中如何使用vba来完成复制粘贴功能
  • Excel怎么查找并返回指定值?
  • Excel中如何使用正则表达式
  • Excel如何用VBA快速提取重复值?
  • 热门搜索
    张国荣为什么要自杀 兖怎么读 扣肉的做法大全 可乐排骨的家常做法 涟漪怎么读 信用卡怎么办理 为什么月经有血块 手机怎么连接电脑 为什么要结婚 脂肪粒怎么消除在眼睛旁边的