我有一个表格条件格式宏(感谢 Jeeped),我想扩展它以遍历一系列工作表中的所有表格。我怀疑这不是最有效的方法,但它是我能拼凑起来的最好的方法,即使那样它也不起作用。到目前为止,我被困在以下两点。非常感谢任何帮助!
1)设置 ws 等于多个工作表代号(例如Set ws = Worksheets(5,6,7)
)
2) 设置没有运行时错误的范围Set myRange = ws.ListObjects.DataBodyRange
会产生“运行时错误'438':对象不支持此属性或方法”
当前代码是:
Sub ConditionalFormatting()
Dim ws As Excel.Worksheet
Dim lo As Excel.ListObject
Dim myRange As Range
Set ws = Worksheet(5) 'Would like to expand to include multiple worksheets!
Set myRange = ws.ListObjects.DataBodyRange
For Each lo In ws.ListObjects
With lo.FormatConditions
.FormatConditions.Delete
Call FormatRange(myRange, 10, "$E5=INDEX(Location,1,1)") 'Warehouse1
Call FormatRange(myRange, 10, "$E5=INDEX(Location,2,1)") 'Warehouse2
Call FormatRange(myRange, 10, "$E5=INDEX(Location,3,1)") 'Warehouse3
End With
Next lo
End Sub
Public Sub FormatRange(r As Range, clr As Integer, frml As String)
r.FormatConditions.Add Type:=xlExpression, Formula1:=frml
r.FormatConditions(r.FormatConditions.Count).Font.colorindex = clr
With r.FormatConditions(1).Borders(xlTop)
.LineStyle = xlContinuous
.Color = color
.TintAndShade = 0
.Weight = xlThin
End With
With r.FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.Color = color
.TintAndShade = 0
.Weight = xlThin
End With
r.FormatConditions(1).StopIfTrue = False
End Sub