Vba Hide/Unhide based on cell Value

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,302
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Guys

I have a sheet named QF - Final Fixtures

I have a cell on that sheet (AF15)

which will have either 3 words (Last 32, Last 16 or Quarter Finalists)

I have 3 columns

32 = AI - AS
16 = AU - BF
Quarter Finalists = BG - BP

What i need is

If cell AF15 says Last 32, then show/Unhide columns AI - AS but Hide columns AU - BF AND BG - BP
If cell AF15 says Last 16, then show/Unhide columns AU - BF but Hide columns AI - AS AND BG - BP
If cell AF15 says Quarter Finalists, then show/Unhide columns BG - BP but Hide columns AI - AS AND AU - BF
If AF15 = "", Then keep all the columns from AI - BP Hidden

I dont want to be pressing a button to hide/unhide. I want it to hide/unhide automatically based on the criteria as above.

Please help

Many Many thanks
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi there,

Something like this in the sheets code (right click sheet tab, view code and paste in the code) should do the trick:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("AF15")) Is Nothing Then
        Select Case Target.Value
            Case "Last 32"
                With ActiveSheet
                    .Columns.Hidden = False
                    .Range("AU:BP").EntireColumn.Hidden = True
                End With
            Case "Last 16"
                With ActiveSheet
                    .Columns.Hidden = False
                    .Range("AI:AS").EntireColumn.Hidden = True
                    .Range("BG:BP").EntireColumn.Hidden = True
                End With
            Case "Quarter Finalists"
                With ActiveSheet
                    .Columns.Hidden = False
                    .Range("AI:AS").EntireColumn.Hidden = True
                    .Range("AU:BF").EntireColumn.Hidden = True
                End With
            Case Else
                With ActiveSheet
                    .Columns.Hidden = False
                    .Range("AI:BP").EntireColumn.Hidden = True
                End With
        End Select
    End If
End Sub
 
Upvote 0
Hi there,

Something like this in the sheets code (right click sheet tab, view code and paste in the code) should do the trick:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("AF15")) Is Nothing Then
        Select Case Target.Value
            Case "Last 32"
                With ActiveSheet
                    .Columns.Hidden = False
                    .Range("AU:BP").EntireColumn.Hidden = True
                End With
            Case "Last 16"
                With ActiveSheet
                    .Columns.Hidden = False
                    .Range("AI:AS").EntireColumn.Hidden = True
                    .Range("BG:BP").EntireColumn.Hidden = True
                End With
            Case "Quarter Finalists"
                With ActiveSheet
                    .Columns.Hidden = False
                    .Range("AI:AS").EntireColumn.Hidden = True
                    .Range("AU:BF").EntireColumn.Hidden = True
                End With
            Case Else
                With ActiveSheet
                    .Columns.Hidden = False
                    .Range("AI:BP").EntireColumn.Hidden = True
                End With
        End Select
    End If
End Sub


Hi Nothing seems to happen

Shall i Hide all the columns first?
 
Upvote 0
Hi there,

Something like this in the sheets code (right click sheet tab, view code and paste in the code) should do the trick:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("AF15")) Is Nothing Then
        Select Case Target.Value
            Case "Last 32"
                With ActiveSheet
                    .Columns.Hidden = False
                    .Range("AU:BP").EntireColumn.Hidden = True
                End With
            Case "Last 16"
                With ActiveSheet
                    .Columns.Hidden = False
                    .Range("AI:AS").EntireColumn.Hidden = True
                    .Range("BG:BP").EntireColumn.Hidden = True
                End With
            Case "Quarter Finalists"
                With ActiveSheet
                    .Columns.Hidden = False
                    .Range("AI:AS").EntireColumn.Hidden = True
                    .Range("AU:BF").EntireColumn.Hidden = True
                End With
            Case Else
                With ActiveSheet
                    .Columns.Hidden = False
                    .Range("AI:BP").EntireColumn.Hidden = True
                End With
        End Select
    End If
End Sub


Hi the only way this works is if i press enter on the cell AF15

AF15 has a formula in there which looks up from another cell on another sheet so will automatically change to either Last 32, Last 16 or Quarter finalists therefore i need it to up date each time cell AF15 changes however i dont want to press enter on that cell for it to work.

Another thing i forgot to mention is that on that sheet Column A - AD are already hidden and i dont want to unhide these columns. At present when i press enter on cell AF15 it unhides the columns from A-AD which i need it to be hidden at all times

Many Many Thanks
 
Upvote 0
Caveats:

The above code has to go in the "QF - Final Fixtures" worksheet module and not in a standard macro module.

The value in AF15 has to be typed-in or selected from a data validation drop down list. It wouldn't work if the value in AF15 is returned from a formula.
 
Upvote 0
Caveats:

The above code has to go in the "QF - Final Fixtures" worksheet module and not in a standard macro module.

The value in AF15 has to be typed-in or selected from a data validation drop down list. It wouldn't work if the value in AF15 is returned from a formula.

Hi Alpha Frog,

The only way this works is if i press enter on the cell AF15
(as you have mentioned it won't work if AF15 is returned via a formula) How can i get round this as AF15 needs to stay as a formula?

AF15 has a formula in there which looks up from another cell on another sheet so will automatically change to either Last 32, Last 16 or Quarter finalists therefore i need it to up date each time cell AF15 changes however i dont want to press enter on that cell for it to work.

Another thing i forgot to mention is that on that sheet Column A - AD are already hidden and i dont want to unhide these columns. At present when i press enter on cell AF15 it unhides the columns from A-AD which i need it to be hidden at all times

Many Many Thanks <!-- / message -->
 
Upvote 0
Just read your most recent post #4. You could have the code run when you make the change to the cell on the other sheet. The one that the formula in AF15 references. What is the sheet name and cell?
 
Upvote 0
Just read your most recent post #4. You could have the code run when you make the change to the cell on the other sheet. The one that the formula in AF15 references. What is the sheet name and cell?


Hi Alpha


Column Y16 - AB26 on sheet (QF - Final Fixtures)

Column Y Column Z Column AA Column AB
<TABLE style="WIDTH: 456pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=608><COLGROUP><COL style="WIDTH: 91pt; mso-width-source: userset; mso-width-alt: 4425" width=121><COL style="WIDTH: 92pt; mso-width-source: userset; mso-width-alt: 4498" width=123><COL style="WIDTH: 93pt; mso-width-source: userset; mso-width-alt: 4534" width=124><COL style="WIDTH: 180pt; mso-width-source: userset; mso-width-alt: 8777" width=240><TBODY><TR style="HEIGHT: 15pt; mso-height-source: userset" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #c00000; WIDTH: 91pt; HEIGHT: 15pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl67 height=20 width=121>Number Of Teams</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #c00000; WIDTH: 92pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl67 width=123>Teams</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #c00000; WIDTH: 93pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl67 width=124>Teams Go Through</TD><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; WIDTH: 180pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl66 width=240>16</TD></TR><TR style="HEIGHT: 15.75pt" height=21><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #f2f2f2; HEIGHT: 15.75pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68 height=21>24</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>10</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>8</TD><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65></TD></TR><TR style="HEIGHT: 16.5pt; mso-height-source: userset" height=22><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; HEIGHT: 16.5pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65 height=22></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>12</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>8</TD><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65></TD></TR><TR style="HEIGHT: 15.75pt" height=21><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; HEIGHT: 15.75pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65 height=21></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>15</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>8</TD><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65></TD></TR><TR style="HEIGHT: 15.75pt" height=21><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; HEIGHT: 15.75pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65 height=21></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>18</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>8</TD><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65></TD></TR><TR style="HEIGHT: 15pt; mso-height-source: userset" height=20><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; HEIGHT: 15pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65 height=20></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>20</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>16</TD><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65></TD></TR><TR style="HEIGHT: 15.75pt" height=21><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; HEIGHT: 15.75pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65 height=21></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>24</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>16</TD><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65></TD></TR><TR style="HEIGHT: 15.75pt" height=21><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; HEIGHT: 15.75pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65 height=21></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>30</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>16</TD><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65></TD></TR><TR style="HEIGHT: 15.75pt" height=21><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; HEIGHT: 15.75pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65 height=21></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>36</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>16</TD><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65></TD></TR><TR style="HEIGHT: 15.75pt" height=21><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; HEIGHT: 15.75pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65 height=21></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>42</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>32</TD><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65></TD></TR><TR style="HEIGHT: 15.75pt" height=21><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; HEIGHT: 15.75pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65 height=21></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>48</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68>32</TD><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: #f2f2f2; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl65></TD></TR></TBODY></TABLE>

In column Y17 on the QF - Final Fixture sheet, i have it equalling to the number of teams i select in sheet named Team-Groups B6 (I have named this Number_Of_Teams

In CELL AB16 I am doing a vlookup to get the amount of teams going through based on the number of teams. which is 16 =VLOOKUP($Y$17,$Z$17:$AA$26,2,0)

Cell AF15 is infact looking at whatever is in cell AB16 (AB16 is on the same sheet QF - Final Fixture sheet) Which is hidden i.e (Columns A - AD) should be hidden at all times.

Is this enough info?
 
Upvote 0
I'm looking for the one cell where you manually make a change. That change will then trigger the columns to Hide\unhide. The hidden columns can still be based on the value in AF15

So what manual change do you make in a cell (any cell) to trigger a change to cell AF15? Do you manually enter a value in Team-Groups B6? If yes, then that would work as the trigger event?

Put this code in the Team-Groups worksheet module...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(0, 0) = "[COLOR="Red"]B6[/COLOR]" Then
        Application.ScreenUpdating = False
        With Sheets("QF - Final Fixtures")
            .Columns("AI:BP").Hidden = True
            Select Case .Range("AF15").Value
                Case "Last 32": .Columns("AI:AS").Hidden = False
                Case "Last 16": .Columns("AU:BF").Hidden = False
                Case "Quarter Finalists": .Columns("BG:BP").Hidden = False
            End Select
        End With
        Application.ScreenUpdating = True
    End If
End Sub
 
Upvote 0
I'm looking for the one cell where you manually make a change. That change will then trigger the columns to Hide\unhide. The hidden columns can still be based on the value in AF15

So what manual change do you make in a cell (any cell) to trigger a change to cell AF15? Do you manually enter a value in Team-Groups B6? If yes, then that would work as the trigger event?

Put this code in the Team-Groups worksheet module...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(0, 0) = "[COLOR=red]B6[/COLOR]" Then
        Application.ScreenUpdating = False
        With Sheets("QF - Final Fixtures")
            .Columns("AI:BP").Hidden = True
            Select Case .Range("AF15").Value
                Case "Last 32": .Columns("AI:AS").Hidden = False
                Case "Last 16": .Columns("AU:BF").Hidden = False
                Case "Quarter Finalists": .Columns("BG:BP").Hidden = False
            End Select
        End With
        Application.ScreenUpdating = True
    End If
End Sub

Hi, i manually change the value in Team-Groups B6 (Shall i change the"B6" to Team-"Groups B6"
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,854
Members
452,948
Latest member
UsmanAli786

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