如何在Excel VBA中使用正则表达式突出显示温度字符串

时间:2019-07-10 06:23:57

标签: regex excel vba

当单元格的内容更改时,某些字符串将突出显示。 这部分我已经开始工作了。 现在,我添加了一个正则表达式来突出显示同时包含温度(即13°或10°°-25°)的字符串。这部分不起作用(没有错误,但没有输出)

这是我到目前为止所拥有的。 除了这部分(blue2Items = "(\d{1,2}°-\d{1,2}°|\d{1,2}°)"

之外,所有其他方法都有效
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("J10:J80")) Is Nothing Then
        Dim objRegex As Object
        Dim RegMC As Object
        Dim RegM As Object
        '-----------------------------------------------------
        Target.Font.ColorIndex = 1
        redItems = "(RXB|RXG|RGX|RXC|RCX|RXD|RXE|RXS|RFG|RNG|RCL|RPG|RFL|RFS|RSC|RFW|ROX|ROP|RPB|RIS|RDS|RRW|RRY|RCM|ICE|MAG|RMD|RLI|RLM|RSB|RBI|RBM|ELI|ELM|CAO)"
        blueItems = "(COL|CRT)"
        greenItems = "(AVI|HEG)"
        blue2Items = "(\d{1,2}°-\d{1,2}°|\d{1,2}°)"
        '-----------------------------------------------------
        allItems = redItems & "|" & blueItems & "|" & blue2Items & "|" & greenItems
        Set objRegex = CreateObject("vbscript.regexp")
        With objRegex
            .Global = True
            .Pattern = allItems
            '-----------------------------------------------------
                'On Error Resume Next
                If .test(Range(Target.Address).Value) Then
                    Set RegMC = .Execute(Range(Target.Address).Value)
                    For Each RegM In RegMC
                        If InStr(redItems, RegM) Then
                            Range(Target.Address).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = RGB(255, 0, 0)
                        ElseIf InStr(blueItems, RegM) Then
                            Range(Target.Address).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = RGB(0, 59, 255)
                        ElseIf InStr(blue2Items, RegM) Then
                            Range(Target.Address).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = RGB(0, 59, 255)
                        ElseIf InStr(greenItems, RegM) Then
                            Range(Target.Address).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = RGB(0, 176, 80)
                        End If
                    Next
                End If
        End With
    End If


I tried the regex with a simple sub and it worked, but I cant get it to work in the above code

    Sub RegExpTemps()

    Dim objRegex As Object
    Dim RegMC As Object
    Dim RegM As Object

    Set objRegex = CreateObject("vbscript.regexp")
    With objRegex
        .Global = True
        .Pattern = "(\d{1,2}°-\d{1,2}°|\d{1,2}°)"
        For row = 10 To 80
            If .test(Cells(row, 10).Value) Then
                Set RegMC = .Execute(Cells(row, 10).Value)
                For Each RegM In RegMC
                    Cells(row, 10).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = RGB(0, 0, 255) 'vbGreen
                Next
            End If
        Next row
    End With
End Sub

预期结果是,当单元格内容发生变化时,包含温度(具有度数字符)的字符串会突出显示

2 个答案:

答案 0 :(得分:1)

blue2Items定义为

blue2Items = "(\d{1,2}°(?:-\d{1,2}°)?)"

更简洁,意思是将1或2位带°的数字匹配,然后将-的可选序列再匹配1或2位带°的数字

然后,您需要根据匹配的捕获组来更改字体颜色。 match.Submatches(x)可让您访问这些值,并且如果您检查它们的长度,就会知道哪一个匹配。

使用

If .test(Range(Target.Address).Value) Then
    Set RegMC = .Execute(Range(Target.Address).Value)
    For Each RegM In RegMC
        If Len(RegM.Submatches(0)) > 0 Then
            Range(Target.Address).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = RGB(255, 0, 0)
        ElseIf Len(RegM.Submatches(1)) > 0 Then
            Range(Target.Address).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = RGB(0, 59, 255)
        ElseIf Len(RegM.Submatches(2)) > 0 Then
            Range(Target.Address).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = RGB(0, 59, 255)
        ElseIf Len(RegM.Submatches(3)) > 0 Then
            Range(Target.Address).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = RGB(0, 176, 80)
        End If
    Next
End If

测试:

enter image description here

答案 1 :(得分:0)

我认为您的表达很好,也许我们会添加一些可选的空格,以防万一,希望它能起作用:

(\d{1,2}(?:\s*)°(?:\s*)-(?:\s*)\d{1,2}(?:\s*)°)\s*|(\d{1,2}(?:\s*)°)

this demo的右上角对表达式进行了说明,如果您想探索/简化/修改它,在this link中,您可以观察它如何与某些示例输入步骤匹配一步一步,如果您喜欢。