【问题标题】:VBA - Changing row shading when a column value changes even with filteredVBA - 当列值更改时更改行底纹,即使已过滤
【发布时间】:2014-05-25 01:52:17
【问题描述】:

我正在尝试编写一个宏来在 B 列中的值更改时更改行的颜色。 A 列将是我使用 1 和 0 的控制列,即只要 B 列保持不变,A 列就会保持 1;每当 B 发生变化时,A 就会翻转为 0,以此类推。

当 B 列中的值发生变化时,我可以让它正确地为行着色,但是当我过滤数据时会出现问题。例如:假设我将 B2-B4 设置为“test1”,B5-B7 设置为“test2”,B8-B10 设置为“test3”,然后我过滤列 B 以不包含“test2”。最初,在列值发生变化的情况下,行的颜色会有所不同,但 B2-B4 和 B8-B10 行设置为相同的颜色,现在它们相互接触,因为“test2”行被隐藏了。

这是我用来为行着色的代码,但它不适用于过滤:

Sub ColorRows()
    Dim This As Long
    Dim Previous As Long
    Dim LastRow As Long
    Dim Color As Integer
    Dim R As Long

    LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    RwColor = Array(15,0)
    Color = 0

    For R = 2 To LastRow
        This = Cells(R, 1).Value
        Previous = Cells(R - 1, 1).Value
        If This <> Previous Then Color = 1 - Color

        Range("A" & R & ":M" & R).Select
        Selection.Interior.ColorIndex = RwColor(Color)
    Next R
End Sub

如何修复它,以便即使在过滤后,当列值发生变化时,行也能正确着色?

【问题讨论】:

  • 您好,欢迎来到堆栈溢出!您可以发布您尝试过的代码吗?这对我们来说更容易查明问题。

标签: vba excel


【解决方案1】:

这是一种方法:

1.) 将以下代码作为 UDF 插入代码模块中。
2.) 然后将公式放入A中,如A2:=analyseVisible(B2)

这会将B-cells 与上面的下一个可见 单元格进行比较,并在A 中产生一个“排名”计数器。

现在 A 中的计数器连续(即使行被隐藏),您可以使用MOD 2 使用条件格式对其进行着色:

3.) 添加条件格式(从 A2 为整个表格):=MOD($A2,2)=1 并设置填充颜色。

如果您现在使用过滤器或更改 B 中的值,则行会实时重新着色。

Public Function analyseVisible(r As Range) As Integer
    Dim i As Long
    If Application.Caller.Row <= 2 Or _
    r.Row <> Application.Caller.Row Then
        analyseVisible = 1
        Exit Function
    End If

    i = r.Row - 1
    While r.Worksheet.Rows(i).Hidden And i > 1
        i = i - 1
    Wend

    If i = 1 Then
        analyseVisible = 1
    Else
        analyseVisible = r.Worksheet.Cells(i, Application.Caller.Column).Value
        If r.Worksheet.Cells(i, r.Column).Value <> _
         r.Value Then analyseVisible = analyseVisible + 1
    End If
End Function

【讨论】:

    【解决方案2】:

    下面的代码通过仅检查已使用和可见的行来处理该问题。它工作得很好,但我无法弄清楚当过滤器改变时如何触发它。它还直接对正在变化的值进行比较。

    Private Sub colorRows()
        Dim this As Variant
        Dim previous As Variant
        Dim currentColor As Long
    
        Dim rng As Range 'visible range
        Dim c As Range ' cell
        ' pick a color to start with
        currentColor = vbYellow
        ' rng = used and visible cells
        Set rng = ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible)
    
        For Each c In rng         ' For each cell that is visible and used
            If Not c.Row = 1 Then ' skip header row
                this = c.Value
                'some simple test logic to switch colors
                If this <> previous Then
                    If currentColor = vbBlue Then
                        currentColor = vbYellow
                    ElseIf currentColor = vbYellow Then
                        currentColor = vbBlue
                    End If
                End If
                'set interior color
                c.Interior.color = currentColor
    
                previous = this
           End If
        Next c
    End Sub
    

    然后,在要着色的工作表模块中,从 Worksheet_Activate() 事件中调用 sub。 (实际上,您可能想要一个不同的活动。我主要与 Access 合作,所以我真的不知道您可以使用什么。我只是想为您指出正确的方向,我确定您的下一个质疑你是否坚持你开始的方法。)

    Private Sub Worksheet_Activate()
        colorRows
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2011-05-14
      • 2021-12-31
      • 2018-10-17
      • 2023-02-06
      • 2021-10-24
      • 2012-06-23
      • 1970-01-01
      • 2013-08-11
      • 2021-02-09
      相关资源
      最近更新 更多