VBA如何在单元格中的用户输入上使用正则表达式

时间:2018-05-29 07:38:21

标签: regex vba

我试图在带有正则表达式的单元格中指定用户输入。我们的想法是只使用这些字符串:

C1, C2, C3, C4, C5, C6, C7, C8, C9, C10

然后是空格,然后合并或完成框架或宽度,最后是空格,然后是1到100之间的整数。

例如 C4合并1,C5宽度2,C7完全框架。完成框架后必须没有整数,并且每3个组合必须用逗号分隔。

到目前为止,我只找到了如何使用特定单词,这就是表达式: ^(?!\ s * $)(?:merge |完成框架|宽度)+ $。这是我正在为我工​​作的表格。目前我只需要在G栏中执行此操作:

 C6,merge,1,  C10,merge,1, C8,merge,1, C9,merge,1, C1,complete framed,1, C6,merge,1, C2,complete framed,1, C5,complete framed,1, C8,merge,1, C1,complete framed,1, C5,merge,1, C5,merge,1, C8,merge,1, C9,merge,1, C7,merge,1, C2,complete framed,1, C6,merge,1, C1,complete framed,1
C5,merge,1, C8,merge,1, C7,merge,1, C9,merge,1, C6,merge,2, C1,complete framed,2, C7,merge,1, C6,merge,1, C8,merge,1, C6,width,1, C8,width,1
C6,merge,1, C4,merge,1 .     

现在的区别在于"完成框架"必须没有整数

 Sub by_blocks_regex()


 Dim strPattern As String: strPattern = "\b(C(?:10|[1-9])),(merge|complete framed|width),(\d+)"
 Dim strReplace As String: strReplace = ""
 Dim regEx As New RegExp
 Dim strInput As String
 Dim strOutput As String
 Dim Myrange As Range
 Dim currMatch As match

 Dim cell As Range
 Set Myrange = ThisWorkbook.Worksheets("BY Blocks").Range("G3:G19")
 Dim currCell As Range

 For Each currCell In Myrange
     If strPattern <> vbNullString Then
         strInput = currCell.Value
     End If
         With regEx
         .Global = True
         .MultiLine = True
         .IgnoreCase = False
         .Pattern = strPattern

     If regEx.Test(strInput) Then
     strOutput = regEx.Replace(strInput, strPattern)
                    'MsgBox ("A cell match with the pattern:   ") &  currCell
                'Else
                   'MsgBox ("No cell match with the pattern:   ") & strPattern
                End If
   End With
   Next currCell
   End Sub

我测试了正则表达式并且它找到了单元格中的每个字符串,但我无法弄清楚如何在单元格输入中使用strPattern并分离每个子字符串,以便只允许有效值,如C5宽度1,C4合并2也许必须使用正则表达式IsMatch方法。当用户在单元格中键入一些数据以查找每个允许的匹配并验证输入数据时。任何人都知道应该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

此代码将在您更新单元格时检查每组三种组合。将代码粘贴到工作表而不是模块中。它假定您的文本遵循以下规则:

  

每3个组合必须用逗号分隔

例如,字符串C6 merge 1, C4 merge 1会检查C6 merge 1C4 merge 1。它忽略逗号和前导/尾随空格。

注意:我还没有检查RegExp模式,因为这不是我的强项,但它会在您输入G列中的数据时进行检查。也许会发布单独的问题获得正确的Reg Exp语法?

您可能希望使用工作表范围创建RegEx变量,该工作表范围在工作表激活时被初始化,并在您离开工作表时被销毁(除非每次更新单元格时都必须这样做)。

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim strPattern As String
    Dim regEx As RegExp
    Dim vValues As Variant
    Dim vValue As Variant

    If Not Intersect(Target, Range("G:G")) Is Nothing Then
        strPattern = "\b(C(?:10|[1-9])),(merge|complete framed|width),(\d+)"
        Set regEx = New RegExp

        vValues = Split(Target, ",")

        With regEx
            For Each vValue In vValues
                '.Global = True 'Not needed as possible matches are passed one at a time using Split.
                '.MultiLine = True
                .IgnoreCase = False
                .Pattern = strPattern

                If .Test(Trim(vValue)) Then
                    MsgBox "Match found in " & Target.Value & " : " & Trim(vValue)
                Else
                    MsgBox "No match"
                End If
            Next vValue
        End With
    End If  

    Set regEx = Nothing

End Sub