roses56
roses56

Reputation: 130

Worksheet Range Reference Not Needed?

The below appears multiple times throughout the script. Is there a reason the the Worksheets(2).UsedRange is used by itself? Would there be a reason to have to keep this here? It looks redundant to me.

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

Upvotes: 0

Views: 49

Answers (2)

ArcherBird
ArcherBird

Reputation: 2134

My guess is that this call to .UsedRange is an attempt to reset the used range of the sheet. I've seen this suggested around the internet (example), though I haven't found it to be a very effective nor consistent way to reset the UsedRange. But, to answer your question, I believe this is what the original author was trying to do.

Upvotes: 1

Damian
Damian

Reputation: 5174

Maybe this explanation helps:

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

Upvotes: 1

Related Questions