Forum

Hide Rows Older Tha...
 
Notifications
Clear all

Hide Rows Older Than 2 Weeks

3 Posts
2 Users
0 Reactions
177 Views
(@amab)
Posts: 15
Eminent Member
Topic starter
 

Hi,

I am new to VBA and attempting to write some code that when run will hide all the rows that contain a date older than 2 weeks, but I keep running into an error in the highlighted row (see below) when testing the code that I'm not sure how to correct. Anyone know what I've done wrong?? There is also an attachment of the file for reference. 

Sub Weeks2Hide()

Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Interface")

wsLR = ws.Cells(Rows.Count,1).End(x1Up).Row

For x = 4 To wsLR

'analyze date, see if it's 2 weeks or older

IF ws.Cells(x,7) <= Date - 14 Then

'hide

ws.Range("g" & x).EntireRows = True

End If

Next x

End Sub

Any help would be greatly appreciated! Thank you!

-Amanda

 
Posted : 02/09/2021 9:03 am
(@purfleet)
Posts: 412
Reputable Member
 

wsLR = ws.Cells(Rows.Count,1).End(x1Up).Row

Firstly it looks like you have a 1 in the xlup part instead of an L

Secondly this is counting the rows in column 1 and you have nothing in column 1 so you need to change to 2

wsLR = ws.Cells(Rows.Count,2).End(xlUp).Row

Also

ws.Range("g" & x).EntireRows = True needs to be ws.Range("g" & x).EntireRow.Hidden = True

 
Posted : 02/09/2021 2:33 pm
(@amab)
Posts: 15
Eminent Member
Topic starter
 

Thank you so much for your help, Purfleet!

I corrected the code to your suggestions and it works great!Smile

 
Posted : 02/09/2021 3:48 pm
Share: