Excel将任意数字替换为空白

目录

问题:替换前单元格内存在很多数字序号,不便于复制使用

解决方法:

1、打开excel,选择宏——》随便起个名字——》录制宏——》直接停止录制

2、选择宏——》查看宏——》选中刚刚命名的宏,点击编辑按钮

3、将代码复制到宏默认方法框架内:

代码:

 Dim RegExp As Object
    Dim SearchRange As Range, Cell As Range
    
    '此处定义正则表达式
    Set RegExp = CreateObject("vbscript.regexp")
    RegExp.Pattern = "[0-9]\d*."
     
    '此处指定查找范围
    Set SearchRange = ActiveSheet.Range("H1:H299")
    
    '遍历查找范围内的单元格
    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

4、修改代码中查找范围:

'此处指定查找范围
Set SearchRange = ActiveSheet.Range(“H1:H299”)

5、修改代码中正则表达式:

RegExp.Pattern = “[0-9]\d*.”

此处表示"."前任意位数的数字

猜你喜欢

转载自blog.csdn.net/weixin_41867184/article/details/125334528