如何更新/更改所有条件规则范围的最后一行
这是我们的条件格式规则:
尽pipe每个AppliesTo
范围的第一行始终是5,但实际情况要复杂得多。
导入新数据后,每个“应用AppliesTo
范围的最后一行可以更改为15。
我们如何使用vba
应用此更改?
这是我们当前的循环,find每个AppliesTo
范围:
Sub updateAllCondFormatRules() Dim x As Excel.FormatCondition Dim r As Range Dim lastRw As Integer Dim newLastRw As Integer newLastRw = 15 For Each x In ActiveSheet.Cells.FormatConditions Set r = x.AppliesTo MsgBox r.Address '>>here we need to change r so that the new last row is equal to newLastRw x.ModifyAppliesToRange r Next x End Sub
我有一个x
对象types的问题,但这个代码似乎工作:
Sub updateAllCondFormatRules() Dim x As Excel.FormatCondition Dim r As Range Dim lastRw As Long Dim newLastRw As Long newLastRw = 20 For Each x In ActiveSheet.Cells.FormatConditions Set r = x.AppliesTo MsgBox r.Address '>>here we need to change r so that the new last row is equal to newLastRw 'Set r = ActiveSheet.Range(Replace(r.Address, Split(r.Address, "$")(UBound(Split(r.Address, "$"))), newLastRw)) Set r = r.Resize(newLastRw - r.Cells(1, 1).Row + 1, r.Columns.Count) MsgBox r.Address x.ModifyAppliesToRange r Next x End Sub