Excel VBA-正则表达式汇总

 

========================

   'Pattern代码汇总
    '^\s 替换行首空格
    '^\n 替换行首的换行符
    '"^\d\.\s*" 去除序号
    '^(.*)$ 匹配整行

========================

Private Sub 批量替换去除无用字符()
    Application.ScreenUpdating = False '关闭屏幕刷新

    Dim RegExp As Object
    Dim SearchRange As Range, Cell As Range
     
    '此处定义正则表达式
    Set RegExp = CreateObject("vbscript.regexp")

    '初始化正则对象
    With RegExp
    .Global = True
    .IgnoreCase = True
    .MultiLine = True
    .Pattern = "^\n"
    End With

    '此处指定查找范围
    Set SearchRange = Selection
     
    '遍历查找范围内的单元格
    For Each Cell In SearchRange
        Set matches = RegExp.Execute(Cell.Value)
        If matches.Count >= 1 Then
            Cell.Value = RegExp.Replace(Cell.Value, "")
        End If
    Next

Application.ScreenUpdating = True '开启屏幕刷新
End Sub

========================

Private Sub 单元格内每行内容添加序号()
    Application.ScreenUpdating = False '关闭屏幕刷新

    Dim RegExp As Object
    Dim SearchRange As Range, Cell As Range
     
    '此处定义正则表达式
    Set RegExp = CreateObject("vbscript.regexp")

    '初始化正则对象
    With RegExp
    .Global = True
    .IgnoreCase = True
    .MultiLine = True
    .Pattern = "^(.*)$"
    End With

    '此处指定查找范围
    Set SearchRange = Selection
     
    '遍历查找范围内的单元格
    For Each Cell In SearchRange
        Set matches = RegExp.Execute(Cell.Value)
         If matches.Count > 1 Then
            For Each Match In matches
                n = n + 1
                strcell = strcell & n & ".  " & Match.Value & Chr(10)
            Next
	'最后一行多了一个Chr(10),需要截去
            Cell.Value = Mid(strcell, 1, Len(strcell) - 1)
        End If
    Next

    Application.ScreenUpdating = True '开启屏幕刷新
End Sub

========================

Public Sub 用空格连接选中单元格内容()
    Dim str As String, temp As String, CXrng As Range, XRrng As Range
    Set CXrng = Selection
 
    For Each XRrng In CXrng
        str = str & Chr(32) & XRrng.Value
        XRrng.ClearContents
    Next
    CXrng(1) = str
End Sub

'连接多个单元格文本,使用Alt+Enter
Public Sub 用Alt+Enter连接选中单元格内容()
    Dim str As String, temp As String, CXrng As Range, XRrng As Range
    Set CXrng = Selection
 
    For Each XRrng In CXrng
        str = str & Chr(10) & XRrng.Value
         XRrng.ClearContents
    Next
     CXrng(1) = str
End Sub

======================

Sub 正则表达式提取匹配文本()
	'定义正则对象和单元格区域
	Dim rngRg As Range
	Dim objRe As Object
	
	'创建正则对象,并将当前选择区域赋值给rngRg
	Set objRe = CreateObject("vbscript.regexp")
	Set rngRg = Selection
	
	'初始化正则对象
	With objRe
	.Global = True
	.IgnoreCase = True
	.MultiLine = True
	.Pattern = "AAA"
	End With
	
	'遍历选择区域的每个单元格
	For Each cell In rngRg
	'如果有符合正则表达式的对象
	    If objRe.test(cell.Value) Then
	'将匹配集合的所有对象的值复制给Matches对象
	        Set Matches = objRe.Execute(cell.Value)
	'遍历Mathces对象,将结果输出到右侧单元格内
	        For countM = 1 To Matches.Count
	            cell.Offset(0, countM) = Matches(countM - 1)
	        Next
	    End If
	Next
End Sub

========================

Sub 正则表达式替换内容输出到右侧列()
	Application.ScreenUpdating = False '关闭屏幕刷新
	'定义正则对象和单元格区域
	Dim rngRg As Range
	Dim objRe As Object
	
	'创建正则对象,并将当前选择区域赋值给rngRg
	Set objRe = CreateObject("vbscript.regexp")
	Set rngRg = Selection
	
	'初始化正则对象
	With objRe
	.Global = True
	.IgnoreCase = True
	.MultiLine = True
	.Pattern = "AAA"
	End With
	
	'遍历选择区域的每个单元格
	For Each cell In rngRg
	'如果有符合正则表达式的对象
	    If objRe.test(cell.Value) Then
	'将匹配集合的所有对象的值进行替换,并输出在右侧一列
	        cell.Offset(0, 1) = objRe.Replace(cell.Value, "BBB")
	    End If
	Next
	
	Application.ScreenUpdating = True'开启屏幕刷新  
End Sub

=======================

=======================

=======================

=======================

=======================

=======================

=======================

=======================

=======================

=======================

=======================

=======================

 

 

 
 
  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Excel VBA 正则表达式可以用于处理文本数据,根据指定的模式匹配并提取所需信息。下面是一个从实例开始的参考模板: 1. 首先,我们需要在 VBA 中启用 "Microsoft VBScript Regular Expressions" 引用。在 VBA 编辑器中,点击 "工具" -> "引用",然后勾选 "Microsoft VBScript Regular Expressions"。 2. 创建一个正则表达式对象并定义要匹配的模式。例如,如果我们想要从一个字符串中提取所有的邮箱地址,我们可以使用以下代码: ```VBA Dim regEx As New RegExp Dim strPattern As String Dim strInput As String strPattern = "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b" strInput = "这是一个字符串,其中包含了多个邮箱地址 test1@test.com, test2@test.com, test3@test.com" With regEx .Global = True .IgnoreCase = True .Pattern = strPattern End With ``` 3. 接下来,我们可以使用正则表达式对象的 Match 方法来查找并获取匹配的结果。例如,我们可以使用循环来提取所有的邮箱地址: ```VBA Dim matches As MatchCollection Set matches = regEx.Execute(strInput) Dim match As Match For Each match In matches MsgBox match.Value Next match ``` 4. 在上述示例中,我们使用了 MsgBox 函数来显示每个匹配的邮箱地址。你可以根据自己的需求进行进一步的处理,例如将匹配的结果存储到一个数组或列表中。 使用正则表达式可以极大地简化文本数据的处理过程,尤其是对于复杂的模式匹配。注意,正则表达式语法很灵活,可以根据不同的需求进行调整。为了更好地理解和使用正则表达式,你可以参考相关的正则表达式教程或手册。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值