Use one macro for multiple buttons to hide individual columns

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I have 12 buttons across E3:P3. Each button would run the same macro "Hide", which simply hides the column. I know I could create 12 separate macros for each button. How can I have one macro "Hide" and for it to know which button the user clicked on thus knowing which column to hide? Each button is named button 1, 2, 3, and so on. So, if my macro "Hide" knew that Button2 was pressed then it would hide columns("F"). I just can't figure out how to pass the name of the button to the "Hide" macro. I thought if I could pass it on I then could use Case Select?

What I came up with to create the buttons is:
VBA Code:
    Dim i As Long, rng As Range
    Set rng = Range("E3")
    For i = 1 To 12
        ActiveSheet.Buttons.Add(rng.Offset(0, i - 1).Left, rng.Offset(0, i - 1).Top, rng.Offset(0, i - 1).Width, rng.Offset(0, i - 1).Height).Select
        Selection.OnAction = "Hide"
        Selection.Characters.Text = "Hide"
        Selection.Name = "Button" & i
    Next i
 
How can I have one macro "Hide"
Different ways possible, one of them is to not pass anything to the VBA procedure but to just use the AlternativeText property of each button​
combined with the Application.Caller function for the button name calling this procedure so nothing is hardcoded …​
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I'd be tempted to use a Worksheet.SelectionChange event to trigger the macro. Something like:

VBA Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Me.Range("E3:P3"), Target) Is Nothing Then
    Selection.Columns.Hidden = True
End If
End Sub

That would hide the column if the user selected any cell in the range E3:P3, and a simple Sub to unhide all the hidden columns:

VBA Code:
Sub unhideColumns()
    ActiveSheet.Cells.Columns.Hidden = False
End Sub

With a shortcut key such as Ctrl+u to run it.
I like the idea. I must not be executing it correctly because nothing is happening when I select E3, or any cell between E3:P3.
 
Upvote 0
See if the following works for you (you can assign macros to shapes as well):
VBA Code:
Sub AddButtons()
    Dim i As Long, rng As Range
    Set rng = Range("E3")
    For i = 1 To 12
        With ActiveSheet.Buttons.Add(rng.Offset(0, i - 1).Left + 1, rng.Offset(0, i - 1).Top + 1, rng.Offset(0, i - 1).Width - 2, rng.Offset(0, i - 1).Height - 2)
            .OnAction = "Hide"
            .Characters.Text = "Hide"
            .Name = "Button" & i
        End With
    Next i
End Sub
Sub Hide()
    ActiveSheet.Buttons(Application.Caller).TopLeftCell.EntireColumn.Hidden = True
End Sub
Worked perfectly, thank you so much. Can you help explain what is happening? It looks very similar to what I already had.
 
Upvote 0
Different ways possible, one of them is to not pass anything to the VBA procedure but to just use the AlternativeText property of each button​
combined with the Application.Caller function for the button name calling this procedure so nothing is hardcoded …​
I've been doing a lot of searching about "Caller". I can't find any information on what all you can put after it. Such as, "TopLeftCell.EntireColumn". I would appreciate it if someone could point me in the right direction on where to find this info. Even in Excel, all it says is Caller is a member of Application. Thanks
 
Upvote 0
Upvote 0
This sample demonstrates how to hide / unhide some columns without hardcoding any, just using Caller & AlternativeText …​
 
Upvote 0
This sample demonstrates how to hide / unhide some columns without hardcoding any …​
Must have been my fat fingers, nothing happens when I click on any of the buttons.
 
Upvote 0
Close the workbook, reopen it and say Yes to activate the Macros …​
 
Upvote 0
Close the workbook, reopen it and say Yes to activate the Macros …​
Odd, the first time I opened it I don't recall being asked to enable macros. Anyhow, up and running. Opened your script, for the life of me I haven't a clue how you did it. I playing around with the text in the button, that didn't make a difference. How in the world do the buttons know which column to hide? You truly are a master.
 
Upvote 0

Forum statistics

Threads
1,214,962
Messages
6,122,482
Members
449,088
Latest member
Melvetica

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