如何 update/change 所有条件规则范围的最后一行

How to update/change last row for all conditional rules' ranges

这是我们的条件格式规则:

尽管每个 AppliesTo 范围的第一行始终为 5,但实际情况要复杂得多。

导入新数据后,每个 AppliesTo 范围的最后一行可以更改为 15。

我们如何使用 vba 应用此更改?

这是我们当前的循环,用于查找每个 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 对象的类型有疑问,但这段代码似乎有效:

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