Macro to amend row height

kerrence

New Member
Joined
Jun 13, 2018
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Hi there,
I am trying to hide rows in a spreadsheet with no success and am hoping a macro will help. I have the first column with the date but do not want any lines which are the following month to show. In the image all rows are needed however for a shorter month they are not. I want to add a macro to hide B34 if the month in B4 does not match. I will also need to run this for B33 and B32.

Can anyone give a total novice a hand? Many thanks!
 

Attachments

  • Screenshot 2021-11-02 144500.png
    Screenshot 2021-11-02 144500.png
    14.3 KB · Views: 18

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hmm.. maybe you can solve this without VBA.
Put this into B5:
Excel Formula:
=IFERROR(IF((B4+1)<=EOMONTH(B4,0),B4+1,""),"")
and drag it down to B34. It should showing only dates which are in month of date from B4.
 
Upvote 0
Hmm.. maybe you can solve this without VBA.
Put this into B5:
Excel Formula:
=IFERROR(IF((B4+1)<=EOMONTH(B4,0),B4+1,""),"")
and drag it down to B34. It should showing only dates which are in month of date from B4.
Hi Kokosek,
The end user wants no rows showing at all annoyingly! I had already used conditional formatting to white out the date if not required but there is formatting in later columns depending if it is a weekend or not so this still shows.
thanks!
 
Upvote 0
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
OK.
So, into Worksheet code put:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
If Not Intersect([B4], Target) Is Nothing Then
    Rows("4:34").Hidden = False
    For Each cell In [B32:B34]
        If Month(cell.Value) <> Month([B4].Value) Then Rows(cell.Row).Hidden = True
    Next cell
End If
End Sub
 
Upvote 0
OK.
So, into Worksheet code put:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
If Not Intersect([B4], Target) Is Nothing Then
    Rows("4:34").Hidden = False
    For Each cell In [B32:B34]
        If Month(cell.Value) <> Month([B4].Value) Then Rows(cell.Row).Hidden = True
    Next cell
End If
End Sub
Thank you for the code but I am not getting it to run. I am uploading my worksheet so any pointers about where I am going wrong would be amazing!
 
Upvote 0
Did you put this code into sheet not into module?
Like this:

Capture.JPG

I suppose that cells B5:B34 are calculated automatically based on starting B4.
If not change this:
VBA Code:
If Not Intersect([B4], Target) Is Nothing Then
into this:
VBA Code:
If Not Intersect([B4:B34], Target) Is Nothing Then
maybe this helps.
 
Upvote 0
Solution
Did you put this code into sheet not into module?
Like this:

View attachment 50385
I suppose that cells B5:B34 are calculated automatically based on starting B4.
If not change this:
VBA Code:
If Not Intersect([B4], Target) Is Nothing Then
into this:
VBA Code:
If Not Intersect([B4:B34], Target) Is Nothing Then
maybe this helps.
You are my hero! My manager tasked me this knowing I am not versed in VBA and I have been struggling with it so much. You are an absolute hero!
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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