0

如果特定列中有任何空白文本,我有一个宏会突出显示一行。此宏用于突出显示用户需要关注的区域。我希望能够通过单击相同的宏按钮在进行更改后取消突出显示这些行。

我该怎么做呢?

这是当前的宏:

Sub Macro13() 
       With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    With ActiveSheet
       .Select
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        .DisplayPageBreaks = False

        Firstrow = 2
        LastRow = .Cells(.Rows.Count, "M").End(xlUp).Row

        For Lrow = LastRow To Firstrow Step -1

             With .Cells(Lrow, "M")
                 If .Value = "" Then
                    .EntireRow.Interior.ColorIndex = 3
                End If
             End With


        Next Lrow
    End With

    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With

End Sub

我的想法是,在宏的开头,检查是否有任何行以红色突出显示。如果是这样,运行一个遍历所有列的新循环,删除单元格突出显示,然后在该循​​环完成后,跳出宏。不过,这很丑陋并且充满了错误。

Sub Macro13() 'Checks for Incorrect Countries

   With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
End With

With ActiveSheet
   .Select
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    .DisplayPageBreaks = False

    Firstrow = 2
    LastRow = .Cells(.Rows.Count, "M").End(xlUp).Row

    FirstrowA = 2
    LastRowA = .Cells(.Rows.Count, "M").End(xlUp).Row

    For Lrow = LastRow To Firstrow Step -1

         With .Cells(Lrow, "M")
             If .EntireRow.Interior.ColorIndex = 3 Then
                   For LrowA = LastRowA To FirstrowA Step -1
                            .EntireRow.Interior.ColorIndex = xlColorIndexNone
                             Next LrowA
                    End
                Exit Sub
             End If


             If .Value = "" Then
                .EntireRow.Interior.ColorIndex = 3
            End If
         End With
    Next Lrow
End With

ActiveWindow.View = ViewMode
With Application
    .ScreenUpdating = True
    .Calculation = CalcMode
End With

End Sub
4

2 回答 2

1

我之前遇到过类似的问题,条件格式对我来说效果不佳。我使用了类似的东西:

Sub CheckAndHighlight(area As Range, Optional ByVal searchValue As String = "")

Application.ScreenUpdating = False

Dim r As Range
For Each r In area
    r.EntireRow.Interior.ColorIndex = 0

    If r.Value = searchValue Then
    r.EntireRow.Interior.ColorIndex = 3
    End If
Next

Application.ScreenUpdating = True

End Sub
于 2013-06-10T19:27:11.920 回答
1

这应该为您解决问题。我添加了一个循环,在开始突出显示空白之前查找任何格式。如果发现红色的东西,它会清除整个红色格式并引发一个标志(Tracker = True)。引发标志时,宏不会将空白单元格的行格式化为红色。我测试了它,它对我有用。

Sub Macro13()
   With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
End With

With ActiveSheet
   .Select
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    .DisplayPageBreaks = False

    Firstrow = 2
    LastRow = .Cells(.Rows.Count, "M").End(xlUp).Row

    Dim Tracker As Boolean
    Tracker = False
    For Lrow = LastRow To Firstrow Step -1
        If .Cells(Lrow, "M").EntireRow.Interior.ColorIndex = 3 Then
            .Cells.Interior.ColorIndex = 0
            Tracker = True
            Exit For
        End If
    Next Lrow

    If Tracker = False Then
        For Lrow = LastRow To Firstrow Step -1

            With .Cells(Lrow, "M")
                If .Value = "" Then
                    .EntireRow.Interior.ColorIndex = 3
                End If
            End With

        Next Lrow
    End If
End With

ActiveWindow.View = ViewMode
With Application
    .ScreenUpdating = True
    .Calculation = CalcMode
End With
End Sub
于 2013-06-11T14:34:59.743 回答