不需要工作表范围参考?

时间:2020-01-31 21:39:27

标签: excel vba

以下内容在脚本中多次出现。是否有单独使用Worksheets(2).UsedRange的原因?是否有必要将其保留在这里?对我来说似乎多余。

Sub Active_Check()

Worksheets(2).Activate

LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

Worksheets(2).UsedRange ' Why is this here?
Worksheets(2).UsedRange.AutoFilter Field:=12, Criteria1:="<=" & Date - 7

2 个答案:

答案 0 :(得分:1)

也许这种解释有帮助:

Sub Active_Check()

    'select the sheet 2
    Worksheets(2).Activate

    'calculates the last row on sheet 2
    LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

    'this is usually used to assign to ranges/arrays. Not needed here.
    Worksheets(2).UsedRange ' Why is this here?

    'everything done before this point is useless because is not used in here.
    'This is filtering on sheet 2 all the data by the column 12 with criteria lesser or equal than
    'Date variable minus seven.
    Worksheets(2).UsedRange.AutoFilter Field:=12, Criteria1:="<=" & Date - 7

答案 1 :(得分:1)

我的猜测是,对.UsedRange的此调用是尝试重置工作表的已用范围。我已经在Internet(example)上看到了这个建议,尽管我还没有发现它是重置UsedRange的非常有效或一致的方法。但是,为回答您的问题,我相信这就是原始作者想要做的。