【问题标题】:How to update/change last row for all conditional rules' ranges如何更新/更改所有条件规则范围的最后一行
【发布时间】:2015-10-20 09:25:59
【问题描述】:

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

实际情况要复杂得多,尽管每个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

【问题讨论】:

    标签: vba excel excel-2013 conditional-formatting


    【解决方案1】:

    我对 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
    

    【讨论】:

    • 好的 - 效果很好(所以uppped):虽然我假设使用OffsetResize的答案会更优雅一点?
    • 呵呵,对不起,简单的脏话,我会考虑resize并尝试找到解决方案! ;)
    • @whytheq :调整大小确实更干净!试试这个 ;)
    • 哈哈 - 以为我问的太多了:谢谢,比乱搞字符串要整洁得多。
    • 没问题,你也没问太多,只是为了让我走出自己的习惯/舒适区,但没那么可怕,别担心;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2021-11-03
    • 2018-05-02
    • 1970-01-01
    相关资源
    最近更新 更多