Home > Enterprise >  Can I assign 1 button to 2 macros in excel. Each macro only to be run when the button is pressed
Can I assign 1 button to 2 macros in excel. Each macro only to be run when the button is pressed

Time:02-04

Can I assign 1 button to 2 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.

Thanks

CodePudding user response:

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

CodePudding user response:

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
  •  Tags:  
  • Related