VBA - Auto Hide or show Column based on a value

shabz1309

New Member
Joined
May 5, 2015
Messages
15
Hi

Seems like my brain isnt working this sunday

can someone please help

i want a VBA script that will auto run in the background to hide or show a column based upon a value in a cell for example

if D1 = 0 ( it should auto hide the column)
if D1 = 1 (it should auto show the column)

Any ideas oh how to do this

Thanks in advance
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
How is the cell value changing?
 
Upvote 0
Here are 2 options choose one that suits u :

1- auto detect using worksheet change event :
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("A1") = 1 Then
Range("A1").EntireColumn.Hidden = True
ElseIf Range("A1") = 0 Then
Range("A:A").EntireColumn.Hidden = False
Else
Exit Sub
End If
End Sub

2- using buttons... etc
sub hideme()
If Range("A1") = 1 Then
Range("A1").EntireColumn.Hidden = True
ElseIf Range("A1") = 0 Then
Range("A:A").EntireColumn.Hidden = False
Else
Exit Sub
End If
end if

change Range("A1") with the range you want .... if needed to be variable declare your range For example :
dim rng as range
rng.entirecolumn.hidden = True / false ... and so on
 
Upvote 0
Try this, in the sheet module
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$A$2" Then
        Columns(4).Hidden = Range("D1").Value
    End If
    

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,693
Members
449,048
Latest member
81jamesacct

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