我為第一組列創建了條件格式(C:E)(見圖 3)。
Range("C1:E36").Select
Selection. Format Conditions. Add Type:=xlExpression, Formulal:="=$D6=""Sun"""
Selection. Format Conditions (Selection. FormatConditions. Count).SetFirstPriority
with Selection. Format Conditions (1). Interior
.Pattern = xlLightVertical
. PatternColor = 65535
.ColorIndex = xlAutomatic
.PatternTintAndShade = 0
End With
我正在嘗試創建一個 for 回圈,它應該應用于所有十二組 - 每組有 3 列(見圖 2)。此外,它應該運行 3 次 - 從行開始C6, C45,C84- 對應于我試圖顯示的三年(見圖 1)。我正在為 for 回圈而苦苦掙扎。以及$D6條件格式中列的相對 abs 參考以及如何使其成為$G6, $J6, $D84, $G84.
For o = 1 TO 3 Step 1
For I = 1 To 12 Step 1
Range (.Cells(6, I * 3), .Cells (36, I * 3 2)).Select
Selection. Format Conditions. Add Type:=xlExpressionFormulal:="=$D6=""Sun"""
Selection. Format Conditions (Selection. Format Conditions. Count).SetFirstPriority
With Selection. Format Conditions (1). Interior
.Pattern = xlLightvertical
.PatternColor = 65535
.ColorIndex = xlAutomatic
.PatternTintAndShade = 0
End With
Next I
Next o
End Sub'



uj5u.com熱心網友回復:
您可以根據需要定義一個 CF 傳遞范圍的 Sub。然后根據需要多次呼叫,以設定所有列
設定條件格式
Sub SetCF(r As Range)
Dim rw As Long
r.FormatConditions.Add Type:=xlExpression, Formula1:="=" & r.Cells(1, 2).Address(False, True) & "=""Sun"""
r.FormatConditions(r.FormatConditions.Count).SetFirstPriority
With r.FormatConditions(1).Interior
.Pattern = xlLightVertical
.PatternColor = 65535
.ColorIndex = xlAutomatic
.PatternTintAndShade = 0
End With
End Sub
呼叫它的范圍
Sub Demo1()
SetCF ActiveSheet.Range("C1:E36")
End Sub
為幾個偏移范圍呼叫它
Sub Demo2()
Dim r As Range
Dim i As Long
Set r = ActiveSheet.Range("C1:E36")
For i = 0 To 11
SetCF r.Offset(, i * 3)
Next
End Sub
uj5u.com熱心網友回復:
對于復制格式,我建議.Copy并.PasteSpecial使用xlPasteFormats. 至于動態確定范圍,由于您的范圍具有規則的大小和可預測的位置,因此最簡單的方法是撰寫靜態For Loop來迭代行號和列號。
Sub Example()
Dim r As Long, c As Long
For r = 6 To 84 Step 39
For c = 3 To 36 Step 3
Cells(r, "C").Resize(38, 3).Copy
Cells(r, c).Resize(38, 3).PasteSpecial xlPasteFormats
Next
Next
End Sub
此代碼將格式從“C6:E44”復制到相鄰列。12 組,每組 3 列寬(例如“F6:H44”、“I6:K44”)。然后它將行號從 6 推進到 45 并再次執行,將“C45:E83”復制到“F45:H83”和其他 11 個列集。然后它從第 45 行前進到第 84 行并再次執行此操作。
回應您關于為每個范圍應用新/自定義格式的評論:
Sub Example()
For r = 6 To 84 Step 39
For c = 3 To 36 Step 3
ApplyFormatting Cells(r, c).Resize(38, 3)
Next
Next
End Sub
Sub ApplyFormatting(InRange As Range)
InRange.FormatConditions.Add Type:=xlExpression, Formula1:="=" & InRange.Cells(1, 2).Address(False, True) & "=""Sun"""
InRange.FormatConditions(InRange.FormatConditions.Count).SetFirstPriority
With InRange.FormatConditions(1).Interior
.Pattern = xlLightVertical
.PatternColor = 65535
.ColorIndex = xlAutomatic
.PatternTintAndShade = 0
End With
End Sub
此程序ApplyFormatting采用每個范圍并將地址用作應用于整個范圍的新格式公式的一部分。
uj5u.com熱心網友回復:
這樣做會簡單得多:
- 創建條件格式,以便向下和橫向復制
- 將格式復制并粘貼到其他位置
做1)定義的條件格式分別對每個小區C6,D6和E6,使用式:=OR(D6="Sat",D6="Sun")。
你現在可以做 2):
- 選擇單元格 C6:E6 并復制
- 選擇一個目標組(比如)F6:H36 和粘貼格式
- 現在將 F6:H36 和粘貼格式復制到每個其他列組
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/363799.html
上一篇:在特定作業表上運行的命令按鈕
