hide and unhide columns based on dropdown selection

rup1234

New Member
Joined
Sep 20, 2018
Messages
12
I have a dropdown in column B and i want to hide and unhide column based on dropdown selection .

For example

B4 - is my dropdown list and it contains numbers like

1
2
12
14
3

now based on condition if user selects less than 12 then it hides the H and I columns and if user selects greater than 12 then it unhides the H and I columns.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I have a dropdown in column B and i want to hide and unhide column based on dropdown selection .

For example

B4 - is my dropdown list and it contains numbers like

1
2
12
14
3

now based on condition if user selects less than 12 then it hides the H and I columns and if user selects greater than 12 then it unhides the H and I columns.
Is this only for H and I and those numbers?
 
Upvote 0
This answers your question but see my next post as well
You have not said what happen if B4 = 12, so you may need to adjust this

Goes in SHEET module (right-click on sheet tab \ View Code \ paste code in that window \ {Alt}{F11} to go back to Excel)
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(0, 0) = "B4" Then
        With Me
            If Target < 12 Then .Range("H:I").Columns.Hidden = True Else .Range("H:I").Columns.Hidden = False
        End With
    End If
End Sub
 
Last edited:
Upvote 0
If you want to use different values in B4 to hide other columns as well then I would use a different approach
- I would not use <
- instead I would unhide ALL columns first and then hide the columns to be hidden for each value of B4
- a separate string is required for each possible value in B4
- note how the column string is constructed

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim colStr As String
    If Target.Address(0, 0) = "B4" Then
        With Me
        'show all columns
            .Cells.Columns.Hidden = False
        'hide the ones that should be hidden based on value in cell
            Select Case Target
                Case 1:     colStr = "F:G,K:K,M:M"
                Case 2:     colStr = "Q:Q"
                Case 14:    colStr = "Z:Z"
                'etc
                Case Else:  Exit Sub
            End Select
            .Range(colStr).Columns.Hidden = True
        End With
    End If
End Sub


The hidden columns are for illustration only to show you how to build the strings for different scenarios and ignore the columns mentioned in your question
 
Last edited:
Upvote 0
This answers your question but see my next post as well
You have not said what happen if B4 = 12, so you may need to adjust this

Goes in SHEET module (right-click on sheet tab \ View Code \ paste code in that window \ {Alt}{F11} to go back to Excel)
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(0, 0) = "B4" Then
        With Me
            If Target < 12 Then .Range("H:I").Columns.Hidden = True Else .Range("H:I").Columns.Hidden = False
        End With
    End If
End Sub


how can i hide only cell value like H4 in that
 
Upvote 0
You said originally that you wanted to hide\unhide columns - but now you are saying you want to hide individual cells :confused:

(As already explained above) complete rows and columns can be hidden BUT individual cells cannot be hidden. So to hide a cell, hide either its column or its row.

There are various ways to hide the value of a cell

One way - make the font colour identical to cell background ...
Code:
With ActiveCell
    .Font.Color = .Interior.Color
End With

To toggle between default font colour and cell background colour ....
Code:
With ActiveCell
    Select Case .Font.Color
        Case 0:         .Font.Color = .Interior.Color
        Case Else:      .Font.Color = 0
    End Select
End With

If the above does not do what you want, please explain what you are trying to achieve
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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