xwgli

在 Excel 中,使用 Alt+F11 快捷键打开 VBA 项目窗口,在左侧的工作表名称上点右键,选择查看代码,即可出出现右侧的代码编辑窗口

image

在代码窗口中输入以下代码:

Private Sub RegExp_Replace()

    Dim RegExp As Object
    Dim SearchRange As Range, Cell As Range
    
    \'此处定义正则表达式
    Set RegExp = CreateObject("vbscript.regexp")
    RegExp.Pattern = "[0-9]{5}"
     
    \'此处指定查找范围
    Set SearchRange = ActiveSheet.Range("A1:A99")
    
    \'遍历查找范围内的单元格
    For Each Cell In SearchRange
        Set Matches = RegExp.Execute(Cell.Value)
        If Matches.Count >= 1 Then
            Set Match = Matches(0)
            Cell.Value = RegExp.Replace(Cell.Value, "")
        End If
    Next

End Sub

根据实际需要替换相应参数,点击 image 运行即可得到效果。

分类:

技术点:

相关文章:

  • 2021-11-20
  • 2021-07-16
  • 2021-12-31
  • 2021-09-24
  • 2021-10-09
猜你喜欢
  • 2021-12-14
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2018-12-10
相关资源
相似解决方案