简体   繁体   中英

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.
For example:

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

Thanks

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM