VBA: Hide rows based on value (date)

Marbella

New Member
Joined
Jul 8, 2012
Messages
13
Hi,

I have the below code to hide rows based on the date in column "C". What I am trying to do is hide rows older than today's date. It's working but not all rows are being hidden.

Any suggestion?

Thank you in advance.

Windows 7, MS 2010.

Code:
Private Sub Hidebtn_Click()
Dim myRg As Range
Set myRg = Range([C1].End(xlDown), [C80].End(xlUp))


For Each cell In myRg
If cell.Value >= Now Then
cell.EntireRow.Hidden = True
End If
Next cell


Set myRg = Nothing
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
In code It is sometimes best to use date versus now; I'd also step thru code and after passing your range assigment (in the immediate window) co the .address to verify three rng is correct.
 
Upvote 0
Thank you Jim for your quick answer. Could you please help me by enhancing the aforementioned code.

Once again, thank you :)

Omar

In code It is sometimes best to use date versus now; I'd also step thru code and after passing your range assigment (in the immediate window) co the .address to verify three rng is correct.
 
Upvote 0
I'd only substitute the text "date" for "now". That alone might do it.

I replaced "now" with "date", but nothing happened at all.

Code:
Private Sub Hidebtn_Click()
Dim myRg As Range
Set myRg = Range([C1].End(xlDown), [C80].End(xlUp))


For Each cell In myRg
If cell.Value = Date Then
cell.EntireRow.Hidden = True
End If
Next cell


Set myRg = Nothing
End Sub
 
Upvote 0
I got it working!

The code below to show dates within a week and hide the rest.

Code:
Private Sub Hidebtn_Click()
Dim cell As Range
For Each cell In Range("C2:C50")
If cell.Value <= Date Or cell.Value >= Date + 6 Then
cell.EntireRow.Hidden = True
End If
Next
End Sub

To show all hidden rows.
Code:
Private Sub Showbtn_Click()
Rows.EntireRow.Hidden = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top