简体   繁体   English

每次点击VBA按钮分配一个不同的宏

[英]Assign a different macro per click of VBA Button

I have an excel button that hides columns "SU" when I click it. 我有一个excel按钮,当我单击它时隐藏“ SU”列。 I want to click the button a 2nd time and it hides columns "PR" and etc. Can you manipulate an excel button per click? 我想第二次单击该按钮,它隐藏了“ PR”等列。您能否每次单击操作一个excel按钮?

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. i将自动被赋值为0。第一次检查i = 0它将自动运行,但是您需要第一次。 Once it sets i = i + 1 then it will do whatever else you want it to do for the second time. 一旦设置了i = i + 1 ,它将第二次执行您想要执行的其他任何操作。 If you want to make it a third time, you can always do Else If i = 1 Then , etc. 如果您想第三次使用它,则始终可以执行Else If i = 1 Then ,等等。

Under the Visual Basic Editor, create a new module if you don't already have one and place this in there. 在Visual Basic编辑器下,如果还没有一个新模块,则将其放置在其中。 Create a button and assign the Button1_Click Macro to the Button. 创建一个按钮,并将Button1_Click宏分配给该按钮。

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. 在您的示例中,您表明您首先希望它隐藏SU,然后再次单击PR。 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: 假设您已将其标记为“ Hide Rows S:U”启动,则可以在VBA中对其进行更改以指示在下次单击时将执行的操作:

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). 一旦开始评估当前状态(使用ElseIf甚至使用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"). 个人说明:我发现“ IF what = true”语句比“ if what = false”更容易理解(从技术上讲,您甚至不必键入“ = 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. 否则,您需要用“ = False”进行评估(就像我第一次演示的那样),但是我发现很难理解。 Your results may vary. 您的结果可能会有所不同。

You indicated that you want these hidden in order: "S:U","P:R","M:O","J:L","G:I" . 您指示要按以下顺序隐藏它们:“ 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? 因此,我从评估最后一种可能性是否为真开始-那是-行G:我已经隐藏了吗? 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. 请记住,逻辑必须牢固,否则您将得到意想不到的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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