Use VBA to hide/unhide rows based on cell values

fhzhkunming

New Member
Joined
Mar 4, 2021
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

I have a simple situation but I couldn't make it working.
I want to hide Rows 29-31 if D28 = "Yes" (Fig.1), and want to hide only Row 31 if D28 ="No" and D29 = "Number" (Fig. 2). In other words, if D28 = "Yes", Rows 29-31 are empty (Actually contents in these rows were invisible by conditional formatting); if D28 = "No" and D29 = "Number", only Row 31 is empty row (contents invisible). BTW, values of D28 and D29 were automatically copied from another sheet. I want this hide/unhide event to be automatically triggered.

Below is my code. Could anyone please help me out?

Thanks in advance.

Frank


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'MsgBox "Changed: " & Target.Address

    Application.EnableEvents = False 'pervent triggering another change event

If Not Intersect(Target, Range("D28")) Is Nothing Then
    If Range("D28").Value = "Yes" Then
                Range("D29:D31").EntireRow.Hidden = True
            ElseIf Range("D28").Value = "No" And Range("D29").Value = "Number" Then
                Range("D31").EntireRow.Hidden = True
            Else
                Range("D29:D31").EntireRow.Hidden = False
    
        End If

End If

Application.EnableEvents = True 're-enable events in the end

End Sub
 

Attachments

  • hide_rows-1.JPG
    hide_rows-1.JPG
    38.3 KB · Views: 17
  • hide_rows-2.JPG
    hide_rows-2.JPG
    46.1 KB · Views: 18

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
In what way isn't it working?
 
Upvote 0
@fhzhkunming, in your code any change of cell D29 isn't captured. How about:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Range("D28:D29")) Is Nothing Then Exit Sub

    If Range("D28").Value = "Yes" Then
        Rows("29:31").Hidden = True

    ElseIf Range("D28").Value = "No" And Range("D29").Value = "Number" Then
        Rows("31").Hidden = True

    Else
        Rows("29:31").Hidden = False

    End If

End Sub

 
Upvote 0
How are those cells being changed?
 
Upvote 0
How is it changed? via formula, macro or something else?
 
Upvote 0
@GWteB, thanks for your code. I tried but no lucky. The rows were not hided/unhided.

Thanks anyway.

Frank
 
Upvote 0
The cause of that might be a premature interruption of your own code. Run the code below once and try again. Be sure to paste the code of my post #4 in de module of the sheet to be affected.
VBA Code:
Sub RunOnce()
    Application.EnableEvents = True
End Sub
 
Upvote 0
How is it changed? via formula, macro or something else?
The cells are changed by manually select value from dropdown list. The dropdown list for D28 from previous sheet has three options, "Yes", "No", and "Choose answer". The dropdown list for D29 from previous sheet also has three options, "Number", "Budget", and "Choose answer". Choose "Yes" or "No" for each cell will get different calculations for other cells via if statements, so as to impact the display of Rows 29:31.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,757
Members
449,094
Latest member
dsharae57

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