[英]Conditional Formatting in VBA, based on functions
I have some (semi) complex conditional formatting to do in Excel. 我在Excel中需要做一些(半)复杂的条件格式设置。 I can get it working no problem using the menu, but I want to do it programmatically because I want it applied across multiple sheets (copy, paste as format does not work). 我可以使用菜单使它正常工作,但是我想以编程方式进行操作,因为我希望将其应用于多个工作表(复制,粘贴,因为格式不起作用)。
I tried recording a macro while I set up the CF to get the code, but it doesn't record that type of entry. 我在设置CF以获取代码时尝试记录宏,但它不记录该类型的条目。
Here are the rules I'm applying, currently to the range: =$1:$65536 这是我正在应用的规则,当前适用于该范围:= $ 1:$ 65536
=OR(COLUMN()=1,COLUMN()=2,ISBLANK($B1))
=AND(LEFT($A1,6)="Base (",A1>100)
=AND(LEFT($A1,6)="Base (",A1>=75,A1<=100)
=AND(LEFT($A1,6)="Base (",A1>=50,A1<75)
=AND(LEFT($A1,6)="Base (",A1<50)
=$B1-9.999 (current cell is less than this value)
Like I said, I can copy this manually by copying a whole column from one sheet to the next, but I can't copy it across all sheets, and I'd like to find a way to do it as a Macro so it can easily be applied to new workbooks (which tend to have 10-15 sheets). 就像我说的,我可以通过将整个列从一张纸复制到下一张纸来手动复制,但是我不能在所有纸上复制它,因此我想找到一种方法来作为宏来做轻松应用于新的工作簿(通常有10到15页)。
Sub DoCFRules()
Dim rng As Range
Set rng = ThisWorkbook.Sheets("Sheet1").Cells
rng.Parent.Activate
rng.Cells(1).Select 'Important!
ApplyCF rng, "=OR(COLUMN()=1,COLUMN()=2,ISBLANK($B1))", RGB(0, 255, 0)
ApplyCF rng, "=AND(LEFT(A1,6)=""Base ("",A1>100)", RGB(255, 0, 0)
'etc...
End Sub
Sub ApplyCF(rng As Range, sFormula As String, clr As Long)
With rng.FormatConditions.Add(Type:=xlExpression, Formula1:=sFormula)
.Interior.Color = clr
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.