Skip to main content

How to hide or unhide columns based on drop down list selection in Excel?

While using Excel, you can hide or unhide specific columns based on the selection of a drop down list. For example, if you select No in the drop down list, column C to I will be hidden, but if you select Yes, the hidden columns C to I will be unhidden. See below screenshot shown.
In this article, we will show you a VBA method to hide or unhide columns based on drop down list selection in Excel.

Hide or unhide columns based on drop down list selection in Excel


Hide or unhide columns based on drop down list selection in Excel

As above example mentioned, to hide or unhide columns C to I based on the drop down list selection, please do as follows.

1. First, create your drop down list with Yes and No which you need.

2. Then press Alt + F11 to open the Microsoft Visual Basic for Application window.

3. Double click the current open sheet name in the VBAProject section to open the Code editor.

4. Then copy and paste below VBA code into the Code editor.

VBA code: hide or unhide columns based on drop down list selection

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180822
    If Target.Column = 2 And Target.Row = 3 Then
        If Target.Value = "No" Then
            Application.Columns("C:I").Select
            Application.Selection.EntireColumn.Hidden = True
        ElseIf Target.Value = "Yes" Then
            Application.Columns("C:I").Select
            Application.Selection.EntireColumn.Hidden = False
        End If
    End If
End Sub

Note: In the above code, Column = 2 and Row = 3 is the cell reference of the drop down list, and the range C:I is the columns that you want to hide or unhide, .please change them to your need.

5. Press Alt + Q keys simultaneously to exit the Microsoft Visual Basic for Application window.

From now on, when you select No in the drop down list, all specified columns are hidden.

But if you select Yes in the drop down list, all hidden columns are displayed immediately.


Related articles:

Best Office Productivity Tools

Supports Office/Excel 2007-2021 and 365  |  Available in 44 Languages  |  Easy to Uninstall Completely

Popular Features: Find/Highlight/Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns     Move Columns   |   Unhide Columns   |   Compare Columns to Select Same & Different Cells ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Kutools for Excel Boasts Over 300 Features, Ensuring That What You Need Is Just A Click Away...

Supercharge Your Excel Skills: Experience Efficiency Like Never Before with Kutools for Excel  (Full-Featured 30-Day Free Trial)

kte tab 201905

60-Day Unconditional Money-Back GuaranteeRead More... Free Download... Purchase... 

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day! (Full-Featured 30-Day Free Trial)
60-Day Unconditional Money-Back GuaranteeRead More... Free Download... Purchase... 
 
Comments (83)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
My dropdown has multiple options which are: Early convos, Mid-negotiations, Currently working, and Rejected. I want to two columns when the Early convos, Mid-negotiations, and Currently working options are selected and show the same two columns when Rejected is selected.

I would like to know how to code the If Target.Value = "Early convos, Mid-negotiations, Currently working" (multiple options).

My current code is below.

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180822
If Target.Column = 7 And Target.Row = 3 Then
If Target.Value = "Early convos,Mid-negotiations,Currently working" Then
Application.Columns("H:I").Select
Application.Selection.EntireColumn.Hidden = True
ElseIf Target.Value = "Rejected" Then
Application.Columns("H:I").Select
Application.Selection.EntireColumn.Hidden = False
End If
End If
End Sub
This comment was minimized by the moderator on the site
Hi,
The following VBA code might help. Please give it a try.
Private Sub Worksheet_Change(ByVal Target As Range)
    'Updated based on your requirements
    If Target.Column = 7 And Target.Row = 3 Then
        Select Case Target.Value
            Case "Early convos", "Mid-negotiations", "Currently working"
                Columns("H:I").EntireColumn.Hidden = True
            Case "Rejected"
                Columns("H:I").EntireColumn.Hidden = False
        End Select
    End If
End Sub
This comment was minimized by the moderator on the site
I used the original code that you posted and edited to fit my needs. However, my drop-down selections are not YES or NO. My choices are: Early convos, Mid-negotiations, Currently Working, and Rejected.

I want Column 13 Row 6 to be hidden when the choices selected are "Early convos, Mid-negotiations, Currently Working" and I want them hidden when the "Rejected" is selected.

How do I add multiple choices in: If Target.Value = "Early convos" and more choices on here?

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180822
If Target.Column = 13 And Target.Row = 6 Then
If Target.Value = "Early convos" Then
Application.Columns("N:O").Select
Application.Selection.EntireColumn.Hidden = True
ElseIf Target.Value = "Rejected" Then
Application.Columns("N:O").Select
Application.Selection.EntireColumn.Hidden = False
End If
End If
End Sub

I hope I explained it good and looking forward to your response.

I appreciate your time and assistance!
This comment was minimized by the moderator on the site
I made it

Private Sub Worksheet_Change(ByVal Target As Range)

Dim xCells As String
xCells = "50:99" 'change this to the row numbers

If Target.Column = 8 And Target.Row = 10 And Target.Value = "No" Then
Application.Worksheets("DOCUMENT FORM").Rows(xCells).Hidden = "True"
Else
Application.Worksheets("DOCUMENT FORM").Rows(xCells).Hidden = "False"
End If

End Sub
This comment was minimized by the moderator on the site
Hello there,

This code worked worked but I wanted to hide "row 50:99" of another worksheet name: "Document Form"
I tried with below code but I'm missing something

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 8 And Target.Row = 7 Then
If Target.Value = "No" Then
Application.Worksheets("DOCUMENT FORM").Rows("50:99").Select
Application.Worksheets("DOCUMENT FORM").Selection.EntireRow.Hidden = True
Else
If Target.Value = "Yes" Then
Application.Worksheets("DOCUMENT FORM").Rows("50:99").Select
Application.Worksheets("DOCUMENT FORM").Selection.EntireRow.Hidden = False
End If
End If
End Sub

Please help.

Thanks in advance.
Rated 5 out of 5
This comment was minimized by the moderator on the site
Hi,

I am trying to use this code twice in one sheet to reveal to different sets of rows based on two different cells. How do I make this work? The code I have is written as follows:

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180822
If Target.Column = 8 And Target.Row = 20 Then
If Target.Value = "No" Then
Application.Rows("21:24").Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Rows("21:24").Select
Application.Selection.EntireRow.Hidden = False
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180822
If Target.Column = 8 And Target.Row = 37 Then
If Target.Value = "No" Then
Application.Rows("38:41").Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Rows("38:41").Select
Application.Selection.EntireRow.Hidden = False
End If
End If
End Sub

Thank you in advance
This comment was minimized by the moderator on the site
Hi Jonathan,
Try the following code.

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20220728
If Target.Column = 8 And Target.Row = 20 Then
If Target.Value = "No" Then
Application.Rows("21:24").Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Rows("21:24").Select
Application.Selection.EntireRow.Hidden = False
End If
End If
If Target.Column = 8 And Target.Row = 37 Then
If Target.Value = "No" Then
Application.Rows("38:41").Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Rows("38:41").Select
Application.Selection.EntireRow.Hidden = False
End If
End If
End Sub
This comment was minimized by the moderator on the site
Thanks for your help
This comment was minimized by the moderator on the site
Hej,

Jeg har forsøgt at bruge din VBA kodning til at skjule bestemte rækker i stedet for kolonner. Jeg vil dog gerne have den til at skjuler rækkerne, i forhold til definerede sektioner fx. "sekt1", grundet jeg har mange sektioner der variere i linje antal.

Jeg har forsøgt mig med følgende kode - dog uden held, og evnerne er sluppet op!

Private Sub Worksheet_Change(ByVal Target As Range)
Dim RangeName As String
RangeName = "sekt1"

If Target.Column = 2 And Target.Row = 9 Then
If Target.Value = "No" Then
Application.Rows("Sekt1").Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Rows("Sekt1").Select
Application.Selection.EntireRow.Hidden = False
End If
End If

End Sub

Kan du være behjælpelig her?
This comment was minimized by the moderator on the site
Hi,
Suppose the range name "sekt1" contains many rows and you want to hide or unhide them depending on the selection of the dropdown list.
The code you provided has been updated. Please give it a try.

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20220506
Dim RangeName As String
RangeName = "sekt1"

If Target.Column = 2 And Target.Row = 9 Then

    If Target.Value = "No" Then
        Application.Range("Sekt1").Select
        Application.Selection.EntireRow.Hidden = True

    ElseIf Target.Value = "Yes" Then
        Application.Range("Sekt1").Select
        Application.Selection.EntireRow.Hidden = False
    End If
End If

End Sub
This comment was minimized by the moderator on the site
Hi!

Great explanation, thanks!
I am very curious if it is possible to connect the drop-down list to specified cell entries, instead of a specified column range. That would make the sheet much more stable when adding new columns, since you won´t have to adapt the code every time a new column is added.

So in the current code the drop-down list is connected to a column range:

Application.Columns("H:K").Select

But would it be possible to let the code search for all columns where the e.g. the top row has a specific entry.
If I would select ´Brocolli´ in the drop down list, the code would show all the columns where Brocolli is written in a specific row (e.g. the top row could be dedicated to these entries)
This comment was minimized by the moderator on the site
Hi zozamis,I am a little confused about your question. Are your columns manually hidden beforehand and you only want to show the columns based on the top cell entry? When ´Brocolli´ is selected in the drop down list, the corresponding columns are displayed. If you switch to another item in the drop down list, just hide the same columns again?Can you to be more specific of your question? Thank you.
This comment was minimized by the moderator on the site
Hi Crystal, what you describe is indeed what I am after! :)
The script now hides/unhides based on a predefined column series (in this example C:I)
<div data-tag="quote">If Target.Value = "No" Then
Application.Columns("C:I").Select
Application.Selection.EntireColumn.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Columns("C:I").Select
Application.Selection.EntireColumn.Hidden = False
I would like to have a script that selects the columns based on the top cell entry, instead of a predefined column series.
As example: when I would select ´brocoli´ in the drop-down list, it would first hide all columns and then unhide all columns where the top cell entry is ´brocoli´, instead of unhiding a pre-defined column series.
So where the old code predefines a ´column series´ like (C:I), the new code would search for a specific to ´cell-entry´ like Brocoli 
By doing this, the script would still work fine when a new column is added in between, and it could also be easier when columns with a certain label are not in a consequent series.
Does that make sence? Thanks!
This comment was minimized by the moderator on the site
Hi zozamis,I am sorry for the late responding. The following VBA code can do you a favor. But it has a limitation that the drop-down list cell must be located in column A of the worksheet. And you need to manually change the drop-down list cell (A3) in the code to your own one. Hope I can help. 
<div data-tag="code">Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20220315
Dim xCRg As Range
Dim xURg As Range
Dim xStr As String
Dim xRg As Range
Dim xFnum As Integer
Dim xBolSU, xBolDA As Boolean
Dim xStr2 As String
Dim xBol As Boolean
Set xURg = ActiveSheet.UsedRange
Set xCRg = xURg.Columns
xStr2 = "Brocolli"
'The drop-down list cell must be located in column A
xStr = Range("A3").Value 'The cell containing the drop-down list
If xStr = xStr2 Then
xBol = False
Else
xBol = True
End If
On Error Resume Next
xBolSU = Application.ScreenUpdating
xBolDA = Application.DisplayAlerts
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For xFnum = 2 To xURg.Columns.Count
Set xRg = xURg.Columns.Item(xFnum)
If xRg.Cells.Item(1).Value = xStr2 Then
xRg.EntireColumn.Select
Application.Selection.EntireColumn.Hidden = xBol
Else
xRg.EntireColumn.Select
Application.Selection.EntireColumn.Hidden = Not xBol
End If
Next
Application.ScreenUpdating = xBolSU
Application.DisplayAlerts = xBolDA
End Sub
This comment was minimized by the moderator on the site
No sorry needed And this is amazing, I will implement this and let you know whether this works in my sheet!

Also, is it possible to apply the script to a given column range, so that some columns are not affecting by the ´hiding filter´
Any work-around to get the drop down in F4 instead of in the A column?

Thanks again!!
This comment was minimized by the moderator on the site
I am attempting to make a tracker for work to track the tasks that I have done. I am lost as to where to go for help but if you know where, or know of someone that can help with how to code I would appreciate the help. Please let me know if this is even possible. 
I have a dropdown in column E with the following selections: ER / SA / RQBased on dropdown list selection, I would like to HIDE the following rows: ER= Hide H-P | SA= Hide F-G & L-P | RQ= Hide F-K
In addition, I would also like to move completed items (Marked "Complete" in Column A) to either the bottom or to a new worksheet titled "Completed".
This comment was minimized by the moderator on the site
Hi any help
how to hide specific column using dropdown and select specific values or text
This comment was minimized by the moderator on the site
Hi,I don't get your point. This article demonstrates the method to hide columns based on the drop-down list selection. Would you try to be more specific about your issue?
This comment was minimized by the moderator on the site
I am using the code below to hide various columns depending on the selection from a drop-down box located in cell C3, but after a calculation is performed anywhere in the worksheet, ALL columns become UNHIDDEN. How do I fix this?

Private Sub Worksheet_Change(ByVal Target As Range)

Columns("D:F").AutoFit

Dim Proj1 As String
Dim Proj2 As String
Dim Proj3 As String
Dim Proj4 As String
Dim Proj5 As String
Dim Proj6 As String
Dim Proj7 As String
Dim Proj8 As String
Dim Proj9 As String
Dim Proj10 As String

Proj1 = ActiveWorkbook.Sheets("Projects").Range("A1").Value
Proj2 = ActiveWorkbook.Sheets("Projects").Range("A2").Value
Proj3 = ActiveWorkbook.Sheets("Projects").Range("A3").Value
Proj4 = ActiveWorkbook.Sheets("Projects").Range("A4").Value
Proj5 = ActiveWorkbook.Sheets("Projects").Range("A5").Value
Proj6 = ActiveWorkbook.Sheets("Projects").Range("A6").Value
Proj7 = ActiveWorkbook.Sheets("Projects").Range("A7").Value
Proj8 = ActiveWorkbook.Sheets("Projects").Range("A8").Value
Proj9 = ActiveWorkbook.Sheets("Projects").Range("A9").Value
Proj10 = ActiveWorkbook.Sheets("Projects").Range("A10").Value

Dim xRG As Range
Dim xHRow As Integer
Set xRG = Range("C3")
If Not Intersect(Target, xRG) Is Nothing Then

If Target.Value = Proj1 Then
Application.Columns("E:F").Hidden = True
Application.Columns("D").Hidden = False

ElseIf Target.Value = Proj2 Then
Range("D:D, F:F").EntireColumn.Hidden = True
Application.Columns("E").Hidden = False

End If
End If
End Sub
This comment was minimized by the moderator on the site
Hi,Can you attach your file here? I tried the code and did some calculations in the worksheet, but the columns are still hidden. We need more details to fix the problem. Sorry for the inconvenience.
This comment was minimized by the moderator on the site
Good day folks, I have data in the worksheet as per the following:


Introduction (Row 1 to 9)

City of Toronto (Row 10-19)

City of Winnipeg(Row20-29)

City of Vancouver(Row 30-39)

There are various data pertaining to each city from column A to Colum J.

There is a dropdown in D3 with city names as stated above. 
I would like to keep rows 10 to 40 hidden by default if no option is selected in cell D3. 
If I select "City of Toronto" then only rows 10-19 should appear, the rest can stay invisible. If I select option "City of Vancouver" then only it should show only rows 30-39.

Row 1 to 9 can stay visible all the time.


Any help achieving this would be highly appreciated.


Thank you so much in advance.
This comment was minimized by the moderator on the site
Here you go. I 'm no expert, but this works for me. I'm sure someone else can give you a cleaner version. Also, you mentioned that you want to keep rows 10-40 hidden but there isn't anything on row 40 (City of Toronto (Row 10-19), City of Winnipeg (Row20-29), City of Vancouver(Row 30-39)). I also attached the file I use to make the code. Feel free to use it and test it.

Private Sub Worksheet_Change(ByVal Target As Range)

Application.ScreenUpdating = False

If Target.Column = 4 And Target.Row = 3 Then

If Target.Value = "" Then

Application.Rows("10:40").Select

Application.Selection.EntireRow.Hidden = True

ElseIf Target.Value = "Toronto" Then

Application.Rows("10:40").Select

Application.Selection.EntireRow.Hidden = True

Application.Rows("10:19").Select

Application.Selection.EntireRow.Hidden = False

ElseIf Target.Value = "Winnipeg" Then

Application.Rows("10:40").Select

Application.Selection.EntireRow.Hidden = True

Application.Rows("20:29").Select

Application.Selection.EntireRow.Hidden = False

ElseIf Target.Value = "Vancouver" Then

Application.Rows("10:40").Select

Application.Selection.EntireRow.Hidden = True

Application.Rows("30:39").Select

Application.Selection.EntireRow.Hidden = False

End If

End If

Application.ScreenUpdating = True

End Sub
This comment was minimized by the moderator on the site
Thank you so much for the response, for some reason i just got the email regarding the response after 6 days.
This works perfectly fine as expected and I was also able to figure this issue out by utilizing Advanced Filter as per following: 
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("D3").Address Then
Range("A10:C250").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("D2:D3")
End If
End Sub

However, there is still another issue. What if cell D3 value coming from another tab? Script doesn't kick in unless i click and hit enter into cell D3. Is there anyway to automate this?
Note: Excel formula options already set to Automatic.
This comment was minimized by the moderator on the site
Hi Najz420,

You would have to place the macro under the worksheet that contains the main cell where one will type in or select (drop-down) the city. Then use something like this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("D3").Address Then
Worksheets("Sheet1").Range("A10:C250").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("D2:D3")
End If
End Sub

** Make sure you edit the worksheet to reference where the data that will be filtered is located. 
Let me know if this works. If you get an error, you might have to change this part: CriteriaRange:=Range("D2:D3")
This comment was minimized by the moderator on the site
THANK YOU SO MUCH, it works perfectly! Feels like huge weight lifted off my shoulder.
This comment was minimized by the moderator on the site
Glad to help! :)
This comment was minimized by the moderator on the site
i need help on, when choose one of the dropdown list example, choose B on the dropdown list then T,U,V column will be hidden
This comment was minimized by the moderator on the site
Hello, I am looking to hide columns based off a list that has "Weekly, Fortnightly and Monthly" as options.
If weekly was selected, no columns to be hidden. If fortnightly is selected columns AA to BA to be hidden. If Monthly is selected columns N to BA to be hidden.
Would somebody be able to point me in the right direction to enter it into excel as shown above.
Further a quick explanation of macros would be handy, otherwise I can look into it myself.
Thankyou!!!!
This comment was minimized by the moderator on the site
Hi, is it possible to do in different sheet
This comment was minimized by the moderator on the site
Good day,
Supposing you want to hide specific rows in Sheet3, please apply the below code. Hope I can help.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim xRG As Range

Dim xHRg As Range

Dim xStrRow As String

Set xRG = Range("B2")

xStrRow = "Sheet3!3:4,Sheet3!7:7,Sheet3!9:10" 'Use comma to separate multiple rows you need to hide or unhide.

On Error Resume Next

If Not Intersect(Target, xRG) Is Nothing Then

If Target.Value = "No" Then

Set xHRg = Application.Range(xStrRow)

xHRg.EntireRow.Hidden = True

ElseIf Target.Value = "Yes" Then

Set xHRg = Application.Range(xStrRow)

xHRg.EntireRow.Hidden = False

End If

End If

End Sub
This comment was minimized by the moderator on the site
Hi Need help with Hide and unhide If its "placed" then column F to I must be visible and Column J to L must be hidden
If its "Higher Education" then column J to L must be visible and Column F to I must be hidden.

Please help in providing code for this.Thanks in advance.
This comment was minimized by the moderator on the site
Hi Timcy,
Try the below VBA. Hope I can help. Thank you!

Private Sub Worksheet_Change(ByVal Target As Range)

Dim xRG As Range

Dim xHRow As Integer

Set xRG = Range("B4")

If Not Intersect(Target, xRG) Is Nothing Then

If Target.Value = "placed" Then

Application.Columns("J:L").Hidden = True

Application.Columns("F:I").Hidden = False

ElseIf Target.Value = "Higher Education" Then

Application.Columns("F:I").Hidden = True

Application.Columns("J:L").Hidden = False

End If

End If

End Sub
This comment was minimized by the moderator on the site
Hi There,

Trying to do something along the same vein as above with an added nuance.

I've created an input sheet with 150 columns for different properties, I'd like to be able to use a data validation dropdown box to select a property and have only that column show up, with the remaining columns hidden.

For reference; category column is B, property columns are C:EX, the property title used in the validation list is in row 156. I have the validation box ready to go and tried augmenting another similar macro I found but looks like I'd have to duplicate the code 149 times over for it to work. I know there has to be a better way than this.

Any help greatly appreciated!
This comment was minimized by the moderator on the site
Hi Thank you for sharing this code. I believe it will work, however the rows I would like to Hide/Unhide are on another worksheet. Can you assist?
This comment was minimized by the moderator on the site
Hi Lisa,
Please try the below VBA.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRG As Range
Dim xHRg As Range
Dim xStrRow As String
Set xRG = Range("B2")
xStrRow = "Sheet3!3:4,Sheet3!7:7,Sheet3!9:10" 'Use comma to separate multiple rows you need to hide or unhide.
On Error Resume Next
If Not Intersect(Target, xRG) Is Nothing Then
If Target.Value = "No" Then
Set xHRg = Application.Range(xStrRow)
xHRg.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Set xHRg = Application.Range(xStrRow)
xHRg.EntireRow.Hidden = False
End If
End If
End Sub
This comment was minimized by the moderator on the site
Hi,

Can you please help me out for Hidden and unHidden for multiple drop down, for example as below

if the drop down in column value is 1 than the column P:AE be Hidden,
if the drop down in column value is 2 than the column T:AE be Hidden,
if the drop down in column Value is 3 than the column X:AE be Hidden,
if the drop down in column Value is 4 than the column AB:AE be Hidden,
if the drop down in column Value is 5 than all column P:AE be UnHidden,


Please let me know how to do the same
This comment was minimized by the moderator on the site
made my whole sheet disappear cuz i am a noob...

here is what i did with your code... because i thought i could switch it for rows...
wanted one pair of rows to disappear and one to appear for "abc" and then the opposite for "xyz"


Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180822
If Target.Column = 6 And Target.Row = 6 Then
If Target.Value = "abc" Then
Application.rows("14:15").Select
Application.Selection.EntireColumn.Hidden = True
Application.Columns("18:19").Select
Application.Selection.EntireColumn.Hidden = false
ElseIf Target.Value = "xyz" Then
Application.Columns("14:15").Select
Application.Selection.EntireColumn.Hidden = False
Application.Columns("18:19").Select
Application.Selection.EntireColumn.Hidden = True
End If
End If
End Sub
This comment was minimized by the moderator on the site
Good day,
You haven't replaced the text "Column" with "Row" in the code. The below code can do you a favor. Thank you for your comment.

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20191128
If Target.Column = 6 And Target.Row = 6 Then
If Target.Value = "abc" Then
Application.Rows("14:15").Select
Application.Selection.EntireRow.Hidden = True
Application.Rows("18:19").Select
Application.Selection.EntireRow.Hidden = False
ElseIf Target.Value = "xyz" Then
Application.Rows("14:15").Select
Application.Selection.EntireRow.Hidden = False
Application.Rows("18:19").Select
Application.Selection.EntireRow.Hidden = True
End If
End If
End Sub
This comment was minimized by the moderator on the site
Thanks for the code, it worked well for 2 items in drop-down list. If I have more than 2 items (say 10) in the drop-down list and have varying columns eg. if A is selected hide columns C-I, if B is selected hide columns B and D-I, if C is selected hide columns B-C and E-I and so on. How do we adjust the code?
Thank you
This comment was minimized by the moderator on the site
Even I have the same question. Any Solution yet?
This comment was minimized by the moderator on the site
I used the code to work great. Thank you. But when I selected from the dropdown it active to last code or moves to the last column data. How to select dropdown every time(Column = 13 And Target.Row = 3 then) not move and can record data next column ?
Thank you.
Example: Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180822
' If Target.Column = 13 And Target.Row = 3 Then
If Target.Column = 13 Then
If Target.Value = "1: Yes" Then
Application.Columns("N:O").Select
Application.Selection.EntireColumn.Hidden = False
Application.Columns("P:S").Select
Application.Selection.EntireColumn.Hidden = True
Application.Columns("V:Z").Select
Application.Selection.EntireColumn.Hidden = True
Application.Columns("X:Z").Select
Application.Selection.EntireColumn.Hidden = True
Application.Columns("AB:AK").Select
Application.Selection.EntireColumn.Hidden = True
Application.Columns("AL").Select
Application.Selection.EntireColumn.Hidden = False
Application.Columns("AM").Select
Application.Selection.EntireColumn.Hidden = True
ElseIf Target.Value = "2: No" Then
Application.Columns("N").Select
Application.Selection.EntireColumn.Hidden = True
Application.Columns("O:Z").Select
Application.Selection.EntireColumn.Hidden = False
Application.Columns("AB:AK").Select
Application.Selection.EntireColumn.Hidden = True
Application.Columns("AL").Select
Application.Selection.EntireColumn.Hidden = True
Application.Columns("AM").Select
Application.Selection.EntireColumn.Hidden = False
ElseIf Target.Value = "" Then
Application.Columns("N:AN").Select
Application.Selection.EntireColumn.Hidden = False
End If
End If
'If Target.Column = 16 And Target.Row = 3 Then
If Target.Column = 16 Then
If Target.Value = "Cat" Then
Application.Columns("V:W").Select
Application.Selection.EntireColumn.Hidden = True
ElseIf Target.Value = "Dog" Then
Application.Columns("V:W").Select
Application.Selection.EntireColumn.Hidden = False
End If
End If
End Sub
This comment was minimized by the moderator on the site
I need helps. Why when I already copied that formula to my VBA, and I tried to play it, the whole table is hidden not just partial column which I want to hide?
Thank you very much for your assistance.
This comment was minimized by the moderator on the site
Hi Reza G.
The code works well in my case. Do you mind attaching a screenshot of your table range and the VBA code after change? Thank you for your comment.
This comment was minimized by the moderator on the site
Bonjour, j'ai le même problème...

Voici mon code VBA modifié :

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 3 And Target.Row = 3 And Target.Value = "Gestion" Then
Application.Columns("D:E").Select
Application.Selection.EntireColumn.Hidden = True

Else

If Target.Value = "Affaire nouvelle" Then
Application.Columns("D:E").Select
Application.Selection.EntireColumn.Hidden = False

Else

If Target.Value = "Avenant" Then
Application.Columns("D:E").Select
Application.Selection.EntireColumn.Hidden = False
End If
End If
End If
End Sub
This comment was minimized by the moderator on the site
I'm getting an error at xHRow = (14:24")

I'm attempting to hide or show multiple rows depending on my C4 selection. Can you assist with what i have wrong?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRG As Range
Dim xHRow As Range
Set xRG = Range("C4")
xHRow = ("14:24")
If Not Intersect(Target, xRG) Is Nothing Then
If Target.Value = "Pulled" Then
Application.Rows(xHRow).Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Replaced" Then
Application.Rows(xHRow).Select
Application.Selection.EntireRow.Hidden = False
End If
End If
End Sub
This comment was minimized by the moderator on the site
The error is: Run-time error '91':
Object variable or with block variable not set
This comment was minimized by the moderator on the site
Each column is a different student ( 2 in this example A and B). I have two dropdowns ( on A1 and B1). Each has "Passed" "Failed" and "Select One" as an option. Now, I managed to make it work when selecting only one dropdown (either A1 or B1). I want to be able to unhide the fields that were hidden by A1 whenever I select something on B1. The goal is to select an option on A1 and fill the rows that are left. Then select B1 and fill whichever rows are left ( Regardless of A1 selection.

Thank you!

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("A1:B2")) Is Nothing Or Target.Cells.Count > 1 Then
Exit Sub

ElseIf Range("A1").Value = "Select One" Then
Rows("2:15").EntireRow.Hidden = False

ElseIf Range("A1").Value = "Passed" Then
Rows("7").EntireRow.Hidden = False
Rows("8:15").EntireRow.Hidden = True

ElseIf Range("A1").Value = "Failed" Then
Rows("7").EntireRow.Hidden = True
Rows("8:15").EntireRow.Hidden = False

ElseIf Range("B1").Value = "Select One" Then
Rows("2:15").EntireRow.Hidden = False

ElseIf Range("B1").Value = "Passed" Then
Rows("7").EntireRow.Hidden = False
Rows("8:15").EntireRow.Hidden = True

ElseIf Range("B1").Value = "Failed" Then
Rows("7").EntireRow.Hidden = True
Rows("8:15").EntireRow.Hidden = False

End If

End Sub
This comment was minimized by the moderator on the site
Good Day,
Sorry can't help you with that. Thank you for your comment.
This comment was minimized by the moderator on the site
Thank you. I used the code and it works great, but when I type into a cell below and press enter it will automatically moves the cursor back to the target box. Is there a way to work around this?
This comment was minimized by the moderator on the site
Hi Grant,
I tried as you mentioned, but didn't find the same problem. Can you tell me your Excel verson? Thanks for commenting.
This comment was minimized by the moderator on the site
The code given to hide/ unhide columns based on value selected from the drop-down list of another column works great. Thanks. But it only works for the one row.
How to apply this for the rest of the selected number of rows in the spreadsheet. I understand we may have to define a variable for the row number and in a loop increment that. But I do not know the syntax for it. Could someone help with that please?
This comment was minimized by the moderator on the site
Hi,
Please try the below VBA code. Hope it can help.
Please change the range as you need.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRG As Range
Set xRG = Range("B3:B30")
If Not Intersect(Target, xRG) Is Nothing Then
If Target.Value = "No" Then
Application.Columns("C:I").Select
Application.Selection.EntireColumn.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Columns("C:I").Select
Application.Selection.EntireColumn.Hidden = False
End If
End If
End Sub
This comment was minimized by the moderator on the site
My dropdown menu is on cell B23. If yes, show and if no, then hide row 29. This is the code I am using:

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180822
If Target.Column = 2 And Target.Row = 23 Then
If Target.Value = "no" Then
Application.Row(29).Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "yes" Then
Application.Row(29).Select
Application.Selection.EntireRow.Hidden = False
End If
End If
End Sub

But I end up with a run-time error 438 - Object doesn't support this property or method. Why? Is it something wrong in the code above?

Thanks in advance for the help.
This comment was minimized by the moderator on the site
Hi Guinther,
The below VBA code can help you solve the problem. Please have a try. Thank you for your comment.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRG As Range
Dim xHRow As Integer
Set xRG = Range("B23")
xHRow = 29
If Not Intersect(Target, xRG) Is Nothing Then
If Target.Value = "No" Then
Application.Rows(xHRow).Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Rows(xHRow).Select
Application.Selection.EntireRow.Hidden = False
End If
End If
End Sub
This comment was minimized by the moderator on the site
Hi Crystal,


I'm hoping you could help me with hiding/showing multiple rows. I would like to show rows 63-73 when YES is selected in cell D51. I'm using the following -


Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRG As Range
Dim xHRow As Integer
Set xRG = Range("D51")
xHRow = ("63:73")
If Not Intersect(Target, xRG) Is Nothing Then
If Target.Value = "No" Then
Application.Rows(xHRow).Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Rows(xHRow).Select
Application.Selection.EntireRow.Hidden = False
End If
End If
End Sub



Thank you!
This comment was minimized by the moderator on the site
Hi Gab,
Please apply the below VBA code. Thank you for your comment.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRG As Range
Dim xHRow As String
Set xRG = Range("D51")
xHRow = "63:73"
If Not Intersect(Target, xRG) Is Nothing Then
If Target.Value = "No" Then
Application.Rows(xHRow).Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Rows(xHRow).Select
Application.Selection.EntireRow.Hidden = False
End If
End If
End Sub
This comment was minimized by the moderator on the site
Hi.

I am going to hide entire row 118 to 120 if Dropdown reads NO otherwise these rows are shown. Can you please help me? My dropdown cell reference at F-117.

Using this code but no actions happened.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 6 And Target.Row = 117 Then
If Target.Value = "No" Then
Application.Rows("118:120").Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Rows("118:120").Select
Application.Selection.EntireRow.Hidden = False
End If
End If
End Sub
This comment was minimized by the moderator on the site
Hi Russ Arnejo,
I'm not sure if I'm understanding this correctly. "Dropdown cell reference at F-117", does this mean that cell F117 contains the drop-down list? If so, just use the cell reference F117 in the code. The whole VBA script is as follows. Please give it a try.

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20230106
Dim xRG As Range
Dim xHRow As String
Set xRG = Range("F117")
xHRow = "118:120"
If Not Intersect(Target, xRG) Is Nothing Then
If Target.Value = "No" Then
Application.Rows(xHRow).Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Rows(xHRow).Select
Application.Selection.EntireRow.Hidden = False
End If
End If
End Sub
This comment was minimized by the moderator on the site
Basically I need to know how to refer to multiple ranges at a time. I tried but it gives error. Here's a look
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 And Target.Row = 4 Then
If Target.Value = "January" Then
Application.Columns("AH:NC").Select
Application.Selection.EntireColumn.Hidden = True
ElseIf Target.Value = " January " Then
Application.Columns("C:AG").Select
Application.Selection.EntireColumn.Hidden = False

ElseIf Target.Value = "February" Then
Application.Columns("C:AG, BJ:NC").Select
Application.Selection.EntireColumn.Hidden = True
ElseIf Target.Value = " February " Then
Application.Columns("AH:BI").Select
Application.Selection.EntireColumn.Hidden = False

End If
End If
End Sub

Note: It works for Target.Value = " January ", but for Target.Value = "February" it highlights error on this line => Application.Columns("C:AG, BJ:NC").Select

Furthermore, once it hides on selecting a target value, it does not unhide on selecting some other target value
This comment was minimized by the moderator on the site
Hi Maria,
Your code has been optimized. Please have a try. Hope I can help.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRG As Range
Dim xHRow As Integer
Set xRG = Range("B4")
If Not Intersect(Target, xRG) Is Nothing Then
If Target.Value = "January" Then
Application.Columns("AH:NC").Hidden = True
Application.Columns("C:AG").Hidden = False
ElseIf Target.Value = "February" Then
Application.Columns("C:AG").Hidden = True
Application.Columns("BJ:NC").Hidden = True
Application.Columns("AH:BI").Hidden = False
End If
End If
End Sub
This comment was minimized by the moderator on the site
Thank you for the helpful article. I would like to take this macro a step further: I need exactly this, but the columns that I need to hide/unhide are not necessarily in a continuous range. My columns range from C to NC. What I want is that if I select Target Value 1, it hides AH to NC, and unhide C to AG, and when I select Target Value 2, it hides C to AG and also BJ to NC, and unhide AH to BI and so on subject to Target value.
This comment was minimized by the moderator on the site
This is exactly what I need to do, but I don't want the column to be highlighted when I hide/unhide. How can I build into the code to go to the next cell?
This comment was minimized by the moderator on the site
I modified this code to hide rows instead of columns, and it works but with one minor problem. I have several data validation lists on the same worksheet, and when I select a value from these other lists, all the rows specified in my code seem to unhide automatically even if the value for the target cell is set to 'No'. Why does this happen and how can I fix it?
This comment was minimized by the moderator on the site
Dear Janice,
Sorry for the mistake, please try the new code below. Thanks for your comment.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 And Target.Row = 3 Then
If Target.Value = "No" Then
Application.Columns("C:I").Select
Application.Selection.EntireColumn.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Columns("C:I").Select
Application.Selection.EntireColumn.Hidden = False
End If
End If
End Sub
This comment was minimized by the moderator on the site
Hi,
Thanks for the code, if I want to add third option what is the syntax?
This comment was minimized by the moderator on the site
Hi there! I have a selection of 10 items and am trying to only show rows 57 to 72 when I select one of them. I am using the following code but somehow it is not working and my dashboard does not change at all. However, when I key in something into a random cell, the page reloads and rows 57:72 become hidden. But then, it stays the same across all my selections and I cannot unhide it again through the drop box selection. May I know if anyone has an alternative suggestion? Thank you in advance!


Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 2 And Target.Row = 3 And Target.Value = "1"

Then Application.Rows("57:72").Select Application.Selection.EntireRow.Hidden = False

Else Application.Rows("57:72").Select Application.Selection.EntireRow.Hidden = True

End If

End Sub
This comment was minimized by the moderator on the site
I need assistance with a similar macro but for specified cell ranges, not a whole column. Can this be done?
This comment was minimized by the moderator on the site
Dear Kara,
Do you mean "hide contents of specified cell ranges based on cell value"? Please post you question with details so as to help us solve the problem.
This comment was minimized by the moderator on the site
I need assistance with excel macro or formula. Can anyone help?
This comment was minimized by the moderator on the site
Can anyone help :


If i enter value to cell A1 as " No", i want columns D:E to hide. and if i enter value as "Yes", i want columns D:E to unhide and columns F:G to hide
This comment was minimized by the moderator on the site
Dear Anish,
This VBA code can help you. Please have a try. Thank you for your comment.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRg As Range
Set xRg = Range("A1")
If xRg.Address = Target.Address And Target.Value = "No" Then
Columns("D:E").EntireColumn.Hidden = True
ElseIf xRg.Address = Target.Address And Target.Value = "Yes" Then
Columns("D:E").EntireColumn.Hidden = False
Columns("F:G").EntireColumn.Hidden = True
End If
End Sub
This comment was minimized by the moderator on the site
I need to use this for Hiding 14 columns alternately based on a cell value.
eg - If A1 = CAT, then hide columns J to V

If A1 = BAT, Then hide columns I and K to V

If A1 = HAT, Then hide columns I, J, and L to P

Basically, my data is in columns I till V and I need hide columns from this range except for the one selected in the reference cell. and unhide all if "All" is selected in the reference cell.

I tried using the same formula above in a loop but I get a "compile error - " Is there a different formula I need to use?
This comment was minimized by the moderator on the site
Dear Ruchi,
Sorry, I don’t really get your point of “Basically, my data is in columns I till V and I need hide columns from this range except for the one selected in the reference cell. and unhide all if "All" is selected in the reference cell.”
This comment was minimized by the moderator on the site
Thank you for the helpful article.


I would like to take this macro a step further: I need exactly this, but with the caveat that the columns that I need to hide are not necessarily in a contiguous range and can be identified by a specific row within each column.

What i have in mind is something like this:
if refcell = "a", then hide all columns with "a" in row 7, else
if refcell = "b", then hide all columns with "b" in row 7, else
if refcell = "c", then hide all columns with "c" in row 7 else
show all columns

If this is possible, how would the VBA code look?
This comment was minimized by the moderator on the site
Dear Ryan,
Please try the following VBA code. When entering "a" into cell A1, all columns with "a" in row 7 will be hidden automatically. It also works when entering b and c in cell A1.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRg As Range
Dim xRgFind As Range
Dim xRgUni As Range
Dim xFirstAddress As String
On Error Resume Next
Application.ScreenUpdating = False
Rows(7).EntireColumn.Hidden = False
If Target.Address = Range("A1").Address Then
Set xRg = Intersect(ActiveSheet.UsedRange, Rows(7))
Set xRgFind = xRg.Find(Target.Value, , xlValues, xlWhole, , , True)
If Not xRgFind Is Nothing Then
xFirstAddress = xRgFind.Address
Do
Set xRgFind = xRg.FindNext(xRgFind)
If xRgUni Is Nothing Then
Set xRgUni = xRgFind
Else
Set xRgUni = Application.Union(xRgUni, xRgFind)
End If
Loop While (Not xRgFind Is Nothing) And (xRgFind.Address <> xFirstAddress)
End If
xRgUni.EntireColumn.Hidden = True
End If
Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Hi Crystal,
I'm new to this. I can't tell from your code how you define a, b, and c as values in cell A1. I basically what to do what your code does, but I need the value of A1 (or any other cell I want to use) to be a string.
Example:
I have a table (range of cells) that contains 1 of 3 values in Row 3. The values are ("Active", "Inactive", and "Closed". I'd like all columns that have "Inactive" or "Closed" in Row 3 to be hidden. The value in Row 3 is from an INDEX/MATCH function that pulls from another sheet in my workbook. In the other sheet I select 1 of the 3 values from a drop down list.

How would I modify this code to look for "Inactive" or "Closed" in Row 3, and only hide those columns?


Many thanks in advance!

Eddie
This comment was minimized by the moderator on the site
Dear Eddie,
Sorry I can't help with this. You can post your question in our forum: https://www.extendoffice.com/forum.html to get more supports from our Excel professional.
This comment was minimized by the moderator on the site
hey! Did you find a solution for this? Looking for the same :D
This comment was minimized by the moderator on the site
Dear John,
The following VBA code can help you solve the problem. When entering "a" into cell A1, all columns with "a" in row 7 will be hidden automatically. It also works when entering b and c in cell A1.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRg As Range
Dim xRgFind As Range
Dim xRgUni As Range
Dim xFirstAddress As String
On Error Resume Next
Application.ScreenUpdating = False
Rows(7).EntireColumn.Hidden = False
If Target.Address = Range("A1").Address Then
Set xRg = Intersect(ActiveSheet.UsedRange, Rows(7))
Set xRgFind = xRg.Find(Target.Value, , xlValues, xlWhole, , , True)
If Not xRgFind Is Nothing Then
xFirstAddress = xRgFind.Address
Do
Set xRgFind = xRg.FindNext(xRgFind)
If xRgUni Is Nothing Then
Set xRgUni = xRgFind
Else
Set xRgUni = Application.Union(xRgUni, xRgFind)
End If
Loop While (Not xRgFind Is Nothing) And (xRgFind.Address <> xFirstAddress)
End If
xRgUni.EntireColumn.Hidden = True
End If
Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Have modified the code like this



Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Column = 3 And Target.Row = 2 And Target.Value = "No"

Then


Application.Rows("3:90").Select


Application.Selection.EntireRow.Hidden = True


Else


Application.Rows("3:90").Select


Application.Selection.EntireRow.Hidden = False


End If


End Sub




But the problem am facing is am unable to answer the yes no : as all the rows 3 to 90 are getting selected not allowing me to answer the next questions
This comment was minimized by the moderator on the site
Dear harikumar,

The code you provided works well for me. When selecting No from the drop-down list (which locates in cell C2), row 3:90 are hidden immediately. And selecting Yes from the drop-down list will unhide them all at once. Sorry i don't get the point of what you said about unable to answer the yes no.
This comment was minimized by the moderator on the site
Hi, I have a workbook and trying to get my head around a VBA code. I have code that if I select from a drop down page1 it unhides a worksheet. I am looking for a code that I select page 1 & select a range of 3-5 from a drop down it unhides a worksheet & unhides 5 columns in the unhidden worksheet. Is that doable?
This comment was minimized by the moderator on the site
getting an error code when you delete the input number, can i get help with this?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations