Hide/Unhide columns with VBA

  • Thread starter Thread starter Mark Jennings
  • Start date Start date
M

Mark Jennings

I want to place a control (check box) on a worksheet that will toggle
column hidden/unhidden when checked/unchecked.
Can anyone help me with some VBA code that will accomplish this
 
Private Sub CheckBox1_Click()
Columns("M:M").hidden = not columns("M:M").hidden
End Sub

This is assuming the control tolbox checkbox. Igf you use a forms checkbox,
createa macro with that code and assign it.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
This will toggle and change the text on the box. You only need the last line
if toggle only.

Sub HideG_UnhideG()
If Columns("g").EntireColumn.Hidden = True Then
Columns("g").EntireColumn.Hidden = False
ActiveSheet.Shapes("toggleit").Select
Selection.Characters.Text = "HIDE G" & Chr(10) & "" & Chr(10) & ""
Range("c3").Select
Else
Columns("g").EntireColumn.Hidden = True
ActiveSheet.Shapes("Toggleit").Select
Selection.Characters.Text = "SHOW G" & Chr(10) & "" & Chr(10) & ""
End If
Range("c3").Select

'can use =not if only ONE change.....
'Columns("g").EntireColumn.Hidden = Not Columns("g").EntireColumn.Hidden
End Sub
 
This information was very helpful.

Is there a way that will associate the state of the contro
(True/False) with the Hide/Unhide condition? Instead of a straigh
toggle?

In other words, a column is hidden if the check box is false, an
unhidden if the checkbox is true
 
Columns("M").Hidden = Not Columns("M").Hidden


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top