简体   繁体   中英

Assign a different macro per click of VBA Button

I have an excel button that hides columns "SU" when I click it. I want to click the button a 2nd time and it hides columns "PR" and etc. Can you manipulate an excel button per click?

One solution to this problem is be declaring a global variable, and assigning a counter to it:

Private i As Integer

Sub Button1_Click()

If i = 0 Then
    Do Stuff 'This is the first click
    i = i + 1
Else
    Do Stuff the Second Time 'This is the second time and beyond.
    i = i + 1
End If

End Sub

i will automatically be assigned a value of 0. The first time it checks i = 0 it will operate however you want the first time. Once it sets i = i + 1 then it will do whatever else you want it to do for the second time. If you want to make it a third time, you can always do Else If i = 1 Then , etc.

Under the Visual Basic Editor, create a new module if you don't already have one and place this in there. Create a button and assign the Button1_Click Macro to the Button.

If you only need to alternate between two options, eg like a toggle on off, then use a boolean:

Public x As Boolean

Private Sub CommandButton1_Click()
If x Then MsgBox ("ON") Else MsgBox ("OFF")
x = Not x
End Sub

If there are just a few things you want it to do (it sounds like it), I would have my macro evaluate the current state and act accordingly.

In your example, you indicate that you first want it to hide SU, then PR on a second click. This will do that:

Sub HideColumns()

    If Sheets("Sheet1").Range("S:S").EntireColumn.Hidden = False Then
        Sheets("Sheet1").Range("S:U").EntireColumn.Hidden = True
    Else
        Sheets("Sheet1").Range("P:R").EntireColumn.Hidden = True
    End If

End Sub

Of course, you can extend this with additional conditions and actions.. like changing the text of the button to represent what it will do next:

Assume you have it labeled "Hide Rows S:U" to start, you can change it inside VBA to indicate what it would do on the next click:

Sub HideColumnsUpdateText()

    If Sheets("Sheet1").Range("S:S").EntireColumn.Hidden = False Then

        Sheets("Sheet1").Range("S:U").EntireColumn.Hidden = True
        Sheets("Sheet1").Buttons("Button 1").Text = "Hide Rows P:R"

    Else
        Sheets("Sheet1").Range("P:R").EntireColumn.Hidden = True

    End If

End Sub

There's really no end to what you can do once you start evaluating the current state (extend using ElseIf, or even use Case). You just have to keep the logic straight.

Edit to further expand: If your situation is linear - that is, if want to hide more and more columns in the same order, you just need to do else ifs to evaluate the situation, and step through the order that you want to hide them.

Personal note: I find that "IF whatever =true" statements are a little easier to follow than "if whatever = false" (and technically, you don't even have to have type the "=True"). But it means you need to start at the last possibility, and work backwards. Otherwise you need to evaluate by "= False" (like I first demonstrated), but I find it a little harder to follow. Your results may vary.

You indicated that you want these hidden in order: "S:U","P:R","M:O","J:L","G:I" . Here is a script that, once all of those rows are hidden, the button would then show them all. So I start by evaluating if the last possibility is true -that is- are Rows G:I hidden already? If so, then show them all. I've also included updating the button text, but that's optional.

Sub hideSetsOfColumnsProgressively()
    ' progressive order of button function: "S:U","P:R","M:O","J:L","G:I","Unhide Rows"
    If Sheets("Sheet1").Range("G:I").EntireColumn.Hidden = True Then
       Sheets("Sheet1").Range("G:U").EntireColumn.Hidden = False 'shows all rows
       'optionally change the text of the button to indicate the next function:
       Sheets("Sheet1").Buttons("Button 1").Text = "Hide Rows S:U"

    ElseIf Sheets("Sheet1").Range("J:L").EntireColumn.Hidden = True Then
    'then we've already hidden all of the other columns, so do the last set
        Sheets("Sheet1").Range("G:I").EntireColumn.Hidden = True
        Sheets("Sheet1").Buttons("Button 1").Text = "Unhide Rows"

    ElseIf Sheets("Sheet1").Range("M:O").EntireColumn.Hidden = True Then
        Sheets("Sheet1").Range("J:L").EntireColumn.Hidden = True
        Sheets("Sheet1").Buttons("Button 1").Text = "Hide Rows G:I"

    ElseIf Sheets("Sheet1").Range("P:R").EntireColumn.Hidden = True Then
        Sheets("Sheet1").Range("M:O").EntireColumn.Hidden = True
        Sheets("Sheet1").Buttons("Button 1").Text = "Hide Rows J:L"

    ElseIf Sheets("Sheet1").Range("S:U").EntireColumn.Hidden = True Then
        Sheets("Sheet1").Range("P:R").EntireColumn.Hidden = True
        Sheets("Sheet1").Buttons("Button 1").Text = "Hide Rows M:O"

    Else
        Sheets("Sheet1").Range("S:U").EntireColumn.Hidden = True
        Sheets("Sheet1").Buttons("Button 1").Text = "Hide Rows P:R"
    End If

End Sub

The nice part about this script is that it loops. You can just keep clicking it and it will hide progressively more and more rows, then show them all.

I hope this answers your question. Keep in mind that the logic has to be solid, or you'll get unexpected results.

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