Hide Unhide Rows

emilemil22

New Member
Joined
Aug 18, 2019
Messages
27
Helloe everyone,

I am completely new to VBA so your help will be appreciated.

I want automatically hide/unhide rows based on cell value. The cell value could have only 2 options: YES or NO

So whenever cell value= YES then hide rows 2:10 and unhide 10:15
whenever cell value= NO then unhide rows 2:10 and hide 10:15

Thanks for your help!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi,
I need you provide more information. What do you mean by whenever? What is the range the algorism should chech the yes/no value?
 
Upvote 0
Thanks for the reply.

Basically I am making a form. User is asked 'Do you have a second citizneship?" If he types in Yes in the cell then multiple rows appear asking him to fill in country, passport number etc.. When the user types in No or leaves it blank then no further questions (rows) appear.

Was I able to explain it?
 
Upvote 0
User tyoes yes or no in one cell is that right?
 
Upvote 0
So whenever user types YES then hide rows 2:10 and unhide 10:15
So whenever user types NO then unhide rows 2:10 and hide 10:15
 
Upvote 0
You do not get me I suppose.
In order to hide/unhide rows 2:10,10:15 I need to know which cell or range of cells I need to check if user types in yes or no value but you have not provided me with such information yet.
 
Upvote 0
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "[COLOR=#ff0000]A1[/COLOR]" Then
      Rows("2:10").Hidden = LCase(Target) = "yes"
      Rows("10:15").Hidden = LCase(Target) <> "yes"
   End If
End Sub
Change the value in red to the cell with the Yes/No answer.
This needs to go in the relevant sheet module
 
Upvote 0
Hi,

The curren active sheet where the rows are to be hidden is Sheet1
The value of YES or NO is types in Cell A2 in different sheet calles Sheet 2
Is this what you needed?

Thankyou very much
 
Upvote 0
In that case try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "A2" Then
      Sheets("Sheet1").Rows("2:10").Hidden = LCase(Target) = "yes"
      Sheets("Sheet1").Rows("10:15").Hidden = LCase(Target) <> "yes"
   End If
End Sub
This needs to go in the sheet module for sheet2
 
Upvote 0
Hi,
Exactly, this is what I was asking about. Here you have the code. Paste it worksheets 2 (not in the module). :)
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws as worksheet
Set ws = worksheets(2)
If Not Intersect(Target, Range("A2")) Is Nothing Then
   ws.Rows("2:10").Hidden = LCase(Target) = "yes"  
 ws.Rows("10:15").Hidden = LCase(Target) <> "yes"

End if
Set ws= nothing
End sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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