VBA to Hide Rows if Month

tlc53

Active Member
Joined
Jul 26, 2018
Messages
412
Office Version
  1. 365
Platform
  1. Windows
Hi. I just can't get this VBA code to work. Basically, if the Year End Date is March, then it needs to hide rows 71:86.
I can't get the Month = 03 bit to work.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("Year_End_Date")) Is Nothing Then

If Target.Cells.CountLarge > 1 Then Exit Sub
Select Case Target.Value

    Case Is = Month("*/03/*")
        Sheets("O7").Range("71:86,").EntireRow.Hidden = True
       
    Case Is <> Month("*/03/*")
        Sheets("O7").Range("71:86").EntireRow.Hidden = False

   
   End Select
   End If
 End Sub

Thank you
 
Changing this part of the macro to this could be a solution:
VBA Code:
Select Case Month(Target.Value)
    Case Is = 3
        Sheets("O7").Range("71:86").EntireRow.Hidden = True
    Case Is <> 3
        Sheets("O7").Range("71:86").EntireRow.Hidden = False
End Select
 
Upvote 0
Solution
What about one of these shorter options?

If "Year_End_Date is on sheet "O7"
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = Range("Year_End_Date").Address Then Rows("71:86").Hidden = Month(Range("Year_End_Date")) = 3
End Sub

If "Year_End_Date is not on sheet "O7"
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = Range("Year_End_Date").Address Then Sheets("O7").Rows("71:86").Hidden = Month(Range("Year_End_Date")) = 3
End Sub
 
Upvote 0
Changing this part of the macro to this could be a solution:
VBA Code:
Select Case Month(Target.Value)
    Case Is = 3
        Sheets("O7").Range("71:86").EntireRow.Hidden = True
    Case Is <> 3
        Sheets("O7").Range("71:86").EntireRow.Hidden = False
End Select
So simple once you know :) Thank you!!
 
Upvote 0
Did you try the other options too?
Hi. Apologies for the delay. Yes, it does work and I can see it's nice and simple with just the one line.
I've saved your code as I'll use it elsewhere but in this particular case, there's another code that runs straight after it. If the entity type <> Company, then those rows and more get hidden. Your code cannot be over-ridden, so it stops this second code from running.
Your code on its own though, functions perfectly. Thanks for your help!
 
Upvote 0

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