首页 > 解决方案 > 使用 VBA 的条件格式 - 代码不起作用

问题描述

我已经编写了以下 VBA 代码来为我的表格重新绘制边框。我还编写了小测试代码,根据 B84 的值在单元格 B11 和 B12 上应用条件格式。如果 B84=1 的值,那么我想在 B11 和 B12 中填充黄色。但它不起作用。

如果这可行,我想编写更多条件,例如,如果 C84=1,则 C11 和 C12 将填充黄色,以此类推,如果 R84=1,则 R11 和 R12 将填充黄色。

Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
ReInstateBorders Range("A7:R16,A18:R23")
Application.ScreenUpdating = True
End Sub

Sub ReInstateBorders(theRange)
For Each are In theRange.Areas
  For rw = 1 To are.Rows.Count - 1 Step 2
    For colm = 1 To are.Columns.Count
      are.Cells(rw, colm).Resize(2).BorderAround xlContinuous
    Next colm
  Next rw
Next are
If Sheets("timetable").Range("B84").Value = 1 Then
    Sheets("timetable").Range("B11").Interior.Color = vbYellow
    Sheets("timetable").Range("B12").Interior.Color = vbYellow
End If
End Sub

标签: excelvba

解决方案


推荐阅读