如何在重叠范围上应用条件格式(VBA)

问题描述 投票:0回答:1

我在 Excel 工作表上应用了不同的条件格式,但是当我使用宏更新数据时,范围发生变化,格式规则成倍增加,因此变得一团糟。 为了避免这种情况,我决定编写一个条件格式代码,该代码将插入到更新工作表上的数据的代码末尾。

唯一的问题是,如果两个范围重叠,格式就会混淆。 代码:

Sub Re_Audit()

Dim wsSmy As Worksheet
Dim LastRow As Integer, DeleteRow As Integer, LastColumn As Integer
Dim StartCells As Range
Dim rng1 As Range, rng2 As Range, rng3 As Range
Set wsSmy = Workbooks("HRDA Audit_Master.xlsb").Sheets("SUMMARY")
Set StartCell = wsSmy.Range("A1")
LastRow = wsSmy.Cells(wsSmy.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = wsSmy.Cells(StartCell.Row, wsSmy.Columns.Count).End(xlToLeft).Column
wsSmy.Activate

Call Macro1 ' - this deletes any existing cond. formatting

Set rng3 = wsSmy.Range("=$I:$J,$L:$L")
With rng3
.FormatConditions.Add Type:=xlExpression, _
Formula1:="='Sheet2'!$G1=""Something"""
.FormatConditions(1).Font.Bold = True
.FormatConditions(1).Font.Color = -709715
.FormatConditions(1).StopIfTrue = False
End With

Set rng1 = wsSmy.Range("$O:$P")
With rng1
.FormatConditions.Add Type:=xlExpression, _
Formula1:="=$Q1=""Something else"""
.FormatConditions(1).Interior.Color = 49407
.FormatConditions(1).StopIfTrue = False
End With

Set rng2 = wsSmy.Range("$I:$K")
With rng2
.FormatConditions.Add Type:=xlExpression, _
Formula1:="='Sheet2'!$F1=""Another thing"""
.FormatConditions(1).Borders(xlLeft).LineStyle = xlDashDot
.FormatConditions(1).Borders(xlLeft).Color = -16777024
.FormatConditions(1).Borders(xlLeft).Weight = xlThin
.FormatConditions(1).Borders(xlTop).LineStyle = xlDashDot
.FormatConditions(1).Borders(xlTop).Color = -16777024
.FormatConditions(1).Borders(xlTop).Weight = xlThin
.FormatConditions(1).Borders(xlRight).LineStyle = xlDashDot
.FormatConditions(1).Borders(xlRight).Color = -16777024
.FormatConditions(1).Borders(xlRight).Weight = xlThin
.FormatConditions(1).Borders(xlBottom).LineStyle = xlDashDot
.FormatConditions(1).Borders(xlBottom).Color = -16777024
.FormatConditions(1).Borders(xlBottom).Weight = xlThin
.FormatConditions(1).StopIfTrue = False
End With

End Sub

如果我仅将 rng2 设置为 K:K,我会得到预期的结果,只是我还需要将格式应用于 I 列和 J 列。因此,如果我将 rng2 设置为 I:K (如上面的代码所示),它将在 I:J 和 L:L 上应用格式,这与 rng3 相同,并且 K 列没有任何格式。我不明白为什么。有人可以帮我分别应用这两种格式吗? 顺便说一句,尽管两种格式都可以格式化 I 和 J 列,但实际上这两种格式不会同时适用于同一行,因此不会发生相互覆盖的情况。

任何帮助将不胜感激。

excel vba conditional-formatting
1个回答
0
投票

之所以如此,是因为在 rng2 中,您将格式化应用于 FormatConditions(1),从而混合了两个规则。为了避免这种情况,请将 1 替换为 2。我稍微缩短了代码,其中在边框上设置了格式:

Sub Re_Audit()

    Dim wsSmy As Worksheet
    Dim LastRow As Integer, DeleteRow As Integer, LastColumn As Integer
    Dim StartCells As Range
    Dim rng1 As Range, rng2 As Range, rng3 As Range
    Set wsSmy = Workbooks("HRDA Audit_Master.xlsb").Sheets("SUMMARY")
    Set StartCell = wsSmy.Range("A1")
    LastRow = wsSmy.Cells(wsSmy.Rows.Count, StartCell.Column).End(xlUp).Row
    LastColumn = wsSmy.Cells(StartCell.Row, wsSmy.Columns.Count).End(xlToLeft).Column
    wsSmy.Activate

    Call Macro1                                  ' - this deletes any existing cond. formatting

    Set rng3 = wsSmy.Range("=$I:$J,$L:$L")
    With rng3
        .FormatConditions.Add Type:=xlExpression, _
                              Formula1:="='Sheet2'!$G1=""Something"""
        .FormatConditions(1).Font.Bold = True
        .FormatConditions(1).Font.Color = -709715
        .FormatConditions(1).StopIfTrue = False
    End With

    Set rng1 = wsSmy.Range("$O:$P")
    With rng1
        .FormatConditions.Add Type:=xlExpression, _
                              Formula1:="=$Q1=""Something else"""
        .FormatConditions(1).Interior.Color = 49407
        .FormatConditions(1).StopIfTrue = False
    End With

    Set rng2 = wsSmy.Range("$I:$K")
    With rng2
        .FormatConditions.Add Type:=xlExpression, _
                              Formula1:="='Sheet2'!$F1=""Another thing"""
        .FormatConditions(2).Borders.LineStyle = xlDashDot
        .FormatConditions(2).Borders.Color = -16777024
        .FormatConditions(2).Borders.Weight = xlThin
        .FormatConditions(2).Borders.Weight = xlThin
        .FormatConditions(2).StopIfTrue = False
    End With

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.