Forum Discussion

SamFares40's avatar
SamFares40
Copper Contributor
Mar 14, 2020
Solved

Hiding Columns Based on a Cell Value

Hello,

 

I added the following VBA into the module and set B4 =0, but it wont hide column H. I am not sure why. the information is in sheet 1

 

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("B4").Value = 0 Then
Columns("H").EntireColumn.Hidden = True
Else
Columns("H").EntireColumn.Hidden = False
End If
End Sub

 

Thanks,

Sam

 

  • SamFares40  your problem is WHERE you put that VBA code.  You put it in Module1, but the Worksheet_SelectionChange needs to be in the sheet it is meant to act on, in this case Sheet1 (expand Microsoft Excel Objects to find it).  I move it to that code sheet and it worked fine.

6 Replies

  • mtarler's avatar
    mtarler
    Silver Contributor

    SamFares40  your problem is WHERE you put that VBA code.  You put it in Module1, but the Worksheet_SelectionChange needs to be in the sheet it is meant to act on, in this case Sheet1 (expand Microsoft Excel Objects to find it).  I move it to that code sheet and it worked fine.

  • Riny_van_Eekelen's avatar
    Riny_van_Eekelen
    Platinum Contributor

    SamFares40 Tried your code, without any modification. Works perfect on my end. BUT.... be aware that when cell B4 is empty it is also seen as -zero- and column H gets hidden. Enter anything else, and column H reappears. 

    • SamFares40's avatar
      SamFares40
      Copper Contributor

      Thank you Riny_van_Eekelen!!

       

      Did you use the excel sheet that i attached. In the screen shot below from the excel sheet that i uploaded earlier,  it is not hiding column "H". What am I doing wrong?

       

      Thanks,

      Sam

       

Resources