Skip to content

Instantly share code, notes, and snippets.

@deerainw
Last active January 14, 2018 08:32
Show Gist options
  • Save deerainw/fc793fb9531ed6a7d5c1ace3e027e649 to your computer and use it in GitHub Desktop.
Save deerainw/fc793fb9531ed6a7d5c1ace3e027e649 to your computer and use it in GitHub Desktop.
excel 中使用正则表达式替换替换满足条件的单元格
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment