WPS Excel啟用正則表達式


WPS Excel啟用正則表達式

新建一個空白表格文件
進入VB編輯器

插入模塊

工具-引用-勾選正則表達式

(Microsoft VBScript Regular Express 5.5)

復制代碼
Option Explicit

Public Function re_sub(sText As String, pattern As String, repl As String)
    Dim oRegExp As Object
    Set oRegExp = CreateObject("vbscript.regexp")
    With oRegExp
        .Global = True 'True表示匹配所有, False表示僅匹配第一個符合項
        .IgnoreCase = False '區分大小寫
        .pattern = pattern
        re_sub = .Replace(sText, repl)
    End With
End Function

Public Function re_find(sText As String, pattern As String)
    Dim oRegExp As Object, match As Object, matches As Object
    Set oRegExp = CreateObject("vbscript.regexp")
    With oRegExp
        .Global = True 'True表示匹配所有, False表示僅匹配第一個符合項
        .IgnoreCase = True '不區分大小寫
        .pattern = pattern
        Set matches = .Execute(sText)
    End With
    
    Dim d As Object
    Set d = CreateObject("Scripting.Dictionary")
    For Each match In matches
        d.Add match, Null
    Next
    re_find = d.keys
End Function


Public Function re_extract(sText As String, pattern As String)
    Dim oRegExp As Object, match As Object, matches As Object, i As Integer
    Set oRegExp = CreateObject("vbscript.regexp")
    With oRegExp
        .Global = True 'True表示匹配所有, False表示僅匹配第一個符合項
        .IgnoreCase = True '不區分大小寫
        .pattern = pattern
        Set matches = .Execute(sText)(0).submatches
    End With
    
    Dim d As Object
    Set d = CreateObject("Scripting.Dictionary")
    For i = 0 To matches.Count - 1
        d.Add matches(i), Null
    Next
    re_extract = d.keys
    
End Function
保存為啟用宏的模板文件

必須使用Excel軟件保存為xlam加載宏的文件格式才可在下一步進行加載

加載宏

在開發工具菜單欄下選擇加載項並選中保存的xlam文件即可加載


關於Excel辦公,推薦方方格子插件
http://www.ffcell.com/home/products.aspx


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM