Can one macro show columns on first run then hide the columns on the second run?

Viewed 59

Can I assign one button to two macros in Excel?

Each macro only to be run when the button is pressed.

For example:

  • First time button is pressed it will show columns.
  • Second time button is pressed it will hide columns.
2 Answers

No you cannot do that. One button can only trigger one procedure.

But you can write a procedure that can toggle the visibility of your columns.

Option Explicit

Public Sub ToggleColumns()
    Dim ColumnsToToggle As Range  ' The range you want to toggle
    Set ColumnsToToggle = ThisWorkbook.Worksheets("Sheet1").Range("A:A")
    
    ColumnsToToggle.EntireColumn.Hidden = Not ColumnsToToggle.EntireColumn.Hidden
End Sub

I would be very surprised if this were possible.

You might opt for something like:

Sub Toggle
  Some_Range.Hidden = not Some_Range.Hidden
End sub
Related