• excel VBA构造正则函数(双参数)


    Function zhengze(ze As String, Rng As Range)
        Set regx = CreateObject("vbscript.regexp")

    With regx

      .Global = True

      .Pattern = ze '写正则表达式
      Set mat = .Execute(Rng)
      For Each mg In mat
         If .test(Rng) Then
            zhengze = mg
         Else
            zhengze = 0
      'Set mat = .Execute(value)

     
          

          'cells 单元格 读取单元格内容,将内容写入单元格,将变量rg的值输入到c列的n行

        End If
       Next

    End With
    End Function

    ------------------------------------------------------------------------------------------------------------------------------------

    if外置,无匹配值则返回空值,单引号后为注释内容,可忽略

    -------------------------------------------------------------------------------------------------------------------------------------

    Function zhengze(ze As String, Rng As Range)
        Set regx = CreateObject("vbscript.regexp")
        

    With regx

      .Global = True

      .Pattern = ze '写正则表达式
      Set mat = .Execute(Rng)
      If .test(Rng) Then '无匹配值则为空
        
        For Each mg In mat
          'If .test(Rng) Then
            zhengze = mg
         
      'Set mat = .Execute(value)


          'cells 单元格 读取单元格内容,将内容写入单元格,将变量rg的值输入到c列的n行

         'End If
        Next
       Else
         zhengze = " "
       End If
    End With
    End Function

    ------------------------------------------------------------------------------------------------------------------------------------

    匹配结果多个时,合并显示

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

    Function zhengze(ze As String, Rng As Range)
        Set regx = CreateObject("vbscript.regexp")
        

    With regx

      .Global = True

      .Pattern = ze '写正则表达式
      Set mat = .Execute(Rng)
      If .test(Rng) Then '无匹配值则为空|匹配成功执行循环
        Dim m As String
        If mat.Count > 1 Then   '为多个匹配结果则合并显示,否则显示当前值
          For Each mg In mat
          
            m = m & mg & "|"    '循环并合并匹配结果
              
          Next
          zhengze = m
        Else
          zhengze = mat(0).value    '参数存储是一个列表形式,不能直接=号取值,必须用列表固有取值方式
        End If
          
          
       Else
         zhengze = " "
       End If
    End With
    End Function

    红尘往事,一切随风!
  • 相关阅读:
    基数排序
    计数排序和桶排序
    部署Java Web项目到云服务器的步骤全解析
    IP地址0.0.0.0/0是什么意思
    Tomcat在阿里云Centos7上正常启动,但浏览器无法访问的解决方法
    eclipse光标怎么返回上一次浏览的位置
    IDEA设置方法自动显示参数提示
    socket通信模型、socket中的accept()阻塞与read()阻塞
    Ubuntu18.04 下修改 root密码
    Ubuntu18.04 安装 VMwareTools
  • 原文地址:https://www.cnblogs.com/xwenwu/p/11760016.html
Copyright © 2020-2023  润新知