VBA 观察值时修改范围

VBA Modify Range when value is observed

我正在尝试让一个 sub 工作,它会根据值 "TRUE" 或 "FALSE" 出现的时间为字段着色。我已经问过下面的问题,并且已经得到了代码,也在下面。

Option Explicit

Public Sub MarkCellsAbove()
Dim ws   As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet2")
Dim v    As Variant
Dim i    As Long, j As Long, n As Long, m As Long, r As Long, y As Long
Dim rng  As Range
Dim rCell As Range
Dim DynamicArea As Range
Dim t    As Double


' get last row in column C
  n = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
' get last column from A
  y = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
' set dynamic area to above values
Set DynamicArea = ws.Range(Cells(1, 1), Cells(n, y))

' clear existing colors over the WHOLE column to minimize file size
  DynamicArea.Interior.ColorIndex = xlColorIndexNone

  For Each rCell In DynamicArea
  Select Case rCell.Text
          Case "TRUE"
            Set rng = rCell.Offset(-2, 0)
            rng.Interior.ColorIndex = 4

        Case "FALSE"
            Set rng = rCell.Offset(-2, 0)
            rng.Interior.ColorIndex = 5

        End Select
    Next


End Sub

这很有效 - 我可以为发现 FALSE 或 TRUE 的上方 2 行的单元格着色。但是 - 我不仅要为这个单元格着色,还要为 Offset 指定范围内的所有单元格着色。所以,如果我在上面指定了 8 个单元格,我想给 8 个单元格上色。

我希望有人能提供帮助 - 我快完成了!

尝试

Set rng = Range(rCell.Offset(-8, 0), rCell.Offset(-1, 0))

请注意,如果 rCell 至少不在第 9 行,您将收到运行时错误