Hide rows based on Datediff

Blanchetdb

Board Regular
Joined
Jul 31, 2018
Messages
161
Office Version
  1. 2016
Platform
  1. Windows
I need some help combining both. I have two cells where dates are entered, so if the difference between both dates is greater than 6 months then rows 71:73 unhide......
 
I assume you meant 2018-04-15 rather than 2108-04-15 :biggrin: anyway try...

Code:
Sub Macro3()
    If Range("K68") > DateSerial(Year(Range("H68")), Month(Range("H68")) + 6, Day(Range("H68"))) Then
        Worksheets("Sheet1").Range("73:75").EntireRow.Hidden = True
    Else
        Worksheets("Sheet1").Range("73:75").EntireRow.Hidden = False
    End If
End Sub
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Here's my attempt (though I'd probably use MARK858's to be honest :)):

Code:
Option Explicit
Sub Macro3()

    Dim intNumOfDays As Integer
    Dim intNumOfMths As Integer
    Dim wsMySheet As Worksheet
    
    Set wsMySheet = ThisWorkbook.Sheets("Sheet1")
    
    intNumOfDays = Day(wsMySheet.Range("K68")) - Day(wsMySheet.Range("H68"))
    intNumOfMths = Evaluate("DATEDIF(" & CStr(wsMySheet.Range("H68").Address) & "," & CStr(wsMySheet.Range("K68").Address) & ",""M"")")

    With wsMySheet
        If intNumOfMths > 6 Then
            .Range("73:75").EntireRow.Hidden = True
        ElseIf intNumOfMths = 6 And intNumOfDays >= 0 Then
            .Range("73:75").EntireRow.Hidden = True
        Else
            .Range("73:75").EntireRow.Hidden = False
        End If
    End With
    
    Set wsMySheet = Nothing
    
End Sub

Hey MARK858,

Congrats on just passing your 10,000th post. Well done!!

Robert
 
Upvote 0
I got it to work thanks to you but now it only works after I "run" the macro.....I need it to happen automatically

this is what I have:

Sub Priority ()

If Range("K68") > DateSerial(Year(Range("H68")), Month(Range("H68")) + 6, Day(Range("H68"))) Then
Worksheets("Sheet1").Range("72:74").EntireRow.Hidden = False
Else
Worksheets("Sheet1").Range("72:74").EntireRow.Hidden = True
End If

If Range("K68") < DateSerial(Year(Range("H68")), Month(Range("H68")) + 6, Day(Range("H68"))) Then
Worksheets("Sheet1").Range("75:76").EntireRow.Hidden = False
Else
Worksheets("Sheet1").Range("75:76").EntireRow.Hidden = True
End If

End Sub

Why isn't it triggered as soon the dates are entered?
 
Upvote 0
Have you put the code in the "View Code" section of the sheet tab. Right click on the tab, click view code and put the code here. It should then trigger everytime the cells referenced are changed.

Thanks,

Chris
 
Upvote 0
Why isn't it triggered as soon the dates are entered?
Because you didn't state it needed to and to do it properly it requires a total rewrite of the code. If I get time I will have a look at it when I knock off work
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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