使用列名作為範圍

使用列名作為範圍

如何在 VBA 中使用列名作為範圍 我使用下面的程式碼 (CTTO) 進行條件格式設置,但是我手動指定了範圍。這次我需要使用列名,因為我想要對多個表格套用條件格式。

    'Definining the variables:
  Dim rng As Range
  Dim condition1 As FormatCondition, condition2 As FormatCondition, condition3 As FormatCondition


 'Fixing/Setting the range on which conditional formatting is to be desired
  Set rng = Range("M5", "M16")


  'To delete/clear any existing conditional formatting from the range
   rng.FormatConditions.Delete


  'Defining and setting the criteria for each conditional format
   Set condition1 = rng.FormatConditions.Add(xlCellValue, xlEqual, "=""""")
   Set condition2 = rng.FormatConditions.Add(xlCellValue, xlGreater, "=report2!$K$1")
   Set condition3 = rng.FormatConditions.Add(xlCellValue, xlLess, "=report2!$K$1")

   rng.FormatConditions(1).StopIfTrue = True
   rng.FormatConditions(1).SetFirstPriority
   rng.FormatConditions(2).StopIfTrue = False
   rng.FormatConditions(3).StopIfTrue = False

   'Defining and setting the format to be applied for each condition
   With condition2
    .Font.Color = -16383844
    .Font.Bold = True
    .Font.TintAndShade = 0
    .Interior.PatternColorIndex = xlAutomatic
    .Interior.Color = 13551615
    .Interior.TintAndShade = 0
   End With

   With condition3
    .Font.ThemeColor = xlThemeColorDark1
    .Font.Bold = True
    .Font.TintAndShade = 0
    .Interior.PatternColorIndex = xlAutomatic
    .Interior.ThemeColor = xlThemeColorAccent6
    .Interior.TintAndShade = -0.499984740745262
   End With

謝謝

相關內容