【问题标题】:Conditional formatting for highlighting top 2 values for each row for visibile cells only仅针对可见单元格突出显示每行的前 2 个值的条件格式
【发布时间】:2017-10-08 19:34:20
【问题描述】:

我正在尝试仅使用 Excel 宏中的条件格式突出显示可见单元格每行的前 2 个值。我的范围是动态的,因此我正在运行一个循环以到达范围的最后一个单元格。

这是我的代码:

With Sheets("pcSupplyChainAnalysis").Select
    For i = 2 To ctr
        Set rng = Range("C" & i & ":" & "I" & i).SpecialCells(xlCellTypeVisible)
        rng.FormatConditions.AddTop10
        rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
        With rng.FormatConditions(1)
            .TopBottom = xlTop10Top
            .Rank = 2
            .Percent = False
        End With
        With rng.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 255
            .TintAndShade = 0
        End With

        rng.FormatConditions(1).StopIfTrue = False
    Next
End With

Ctr 是一个计数器,我正在运行以查找最后一个非空白单元格的位置,因为我的数据也有空白值,我正在使用宏从另一张表中复制它。

ctr = 2
Do While (ActiveSheet.Range("A" & ctr).Value <> "")
    ctr = ctr + 1

Loop
ctr = ctr - 1
ActiveSheet.Range("B2:I" & ctr).Select
Selection.Cut
Range("C2:J" & ctr).Select
ActiveSheet.Paste

附件是我的数据格式的图片。我想突出显示每行的前 2 个数字,并且仅用于可见单元格(因为我也在该范围内使用一些过滤器)。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    试试这个:


    Option Explicit
    
    Public Sub ShowTop2()
        Dim rng As Range, visibleRow As Range
    
        Application.ScreenUpdating = False
    
        With ThisWorkbook.Worksheets("pcSupplyChainAnalysis")
            .Columns.FormatConditions.Delete
            Set rng = .UsedRange.SpecialCells(xlCellTypeVisible)
        End With
    
        For Each visibleRow In rng.Rows
            If visibleRow.Row > 1 Then
                With visibleRow.FormatConditions
                    .AddTop10
                    .Item(.Count).SetFirstPriority
                    With .Item(1)
                        .TopBottom = xlTop10Top
                        .Rank = 2
                        .Interior.Color = 255
                    End With
                End With
            End If
        Next
    
        Application.ScreenUpdating = True
    End Sub
    

    确定 A 列中最后使用的行的更简单方法:

    ctr = Worksheets("pcSupplyChainAnalysis").Cells(Rows.Count, "A").End(xlUp).Row

    您的任何操作都不需要Select 任何东西

    【讨论】:

      猜你喜欢
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多