簡體   English   中英

使用VBA將條件格式應用於一系列單元格

[英]Using VBA to apply conditional formatting to a range of cells

我想知道如何以標題為“適用於”的條件格式訪問該列並輸入我自己的條件。 我已經包含了截圖以供更好的參考。

適用於列

我在條件格式中添加語法的代碼是,

With Selection
  .FormatConditions.Delete
  .FormatConditions.Add Type:=xlExpression, Formula1:="=" & c.Address & "=TRUE"
  .
  .
  .
End With

我相信應該在那里添加代碼,但我找不到正確的語法。

更新:

我更新了我的代碼,看起來像這樣,

With Range(Cells(c.Row, "B"), Cells(c.Row, "N"))
  .FormatConditions.Delete
  .FormatConditions.Add Type:=xlExpression, Formula1:="=" & c.Address
  .FormatConditions(1).Interior.ColorIndex = 15 'change for other color when ticked
End With

這基本上會使特定范圍的行與我放置復選框的位置相關,其背景顏色會發生變化。 復選框位置由c.Address表示,其中'c'包含我選中放置復選框的單元格的位置。

你需要做這樣的事情( Range("A25")正是你要找到的):

With Range("A25")
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, _
            Formula1:="=" & c.Address 
        '.
        '.
        '.
End With

並且不需要寫"=" & c.Address & "=TRUE" ,您只需使用"=" & c.Address

“適用於”是執行With塊的選擇中固有的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM