简体   繁体   English

使用循环基于选中的复选框在Excel中运行某些宏

[英]Using loop to run certain macros in Excel based on checked check boxes

I have a spreadsheet template that needs to run different macros based on which check boxes are ticked before a button is pressed. 我有一个电子表格模板,该模板需要运行不同的宏,这些宏是基于在按下按钮之前打勾的复选框。 I wrote this in an if statement which was fine with a couple of check boxes but gets exponentially longer as to the number of ifs you need to build in to work out all the scenarios. 我在if语句中编写了此语句,该语句可以带有几个复选框,但是对于构建所有方案所需的ifs数量却成倍增加。 Right now there are 6 boxes that could be checked and this should run for all the possibilities. 现在有6个可以检查的框,应该可以使用所有可能的框。

Let's keep it simple and call the check boxes CB1 - CB6 and they should run corresponding macros M1 - M6 depending on which CB's are checked, in any combination (ie, you could choose to run CB1 and CB5 to run M1 and M5, or you could choose all 6 to run all 6 macros). 让我们保持简单,并调用复选框CB1-CB6,它们应该运行相应的宏M1-M6,具体取决于所检查的CB,可以使用任意组合(即,您可以选择运行CB1和CB5来运行M1和M5,或者可以选择全部6个来运行全部6个宏)。

Right now it looks like: 现在看起来像:

Sub Checkboxes()
If ActiveSheet.CB("CB1").Value = 1_
and ActiveSheet.CB("CB2").Value = 1 Then
Call M1
Call M2
ElseIf ActiveSheet.CheckBoxes"CB1").Value = 1_
And ActiveSheet.CheckBoxes("CB2").Value = 0 Then
Call M1
ElseIf ActiveSheet.CheckBoxes("CB2").Value = 1_
And ActiveSheet.CheckBoxes("CB1").Value = 0 Then
Call M2
Else: MsgBox "Please select at least one option to proceed."
End If
End Sub

But you can see how writing an if statement for every scenario gets VERY long and surely not the best way to write it. 但是您会看到为每种情况编写if语句的时间都非常长,而且肯定不是最佳的编写方式。

It seems you may be over complicating things (or I'm not entirely sure what you're after). 看来您可能使事情复杂化了(或者我不太确定您要做什么)。 If there are 6 checkboxes and if checkbox1 is ticked, run macro1. 如果有6个复选框,并且勾选了checkbox1,则运行macro1。 checkbox2 is ticked, run macro 2, etc then you can just do something like: 选中checkbox2,运行宏2,依此类推,您可以执行以下操作:

If ActiveSheet.CB("CB1").Value = 1 then Call M1
If ActiveSheet.CB("CB2").Value = 1 then Call M2
If ActiveSheet.CB("CB3").Value = 1 then Call M3
If ActiveSheet.CB("CB4").Value = 1 then Call M4
If ActiveSheet.CB("CB5").Value = 1 then Call M5
If ActiveSheet.CB("CB6").Value = 1 then Call M6

Right? 对? There shouldn't be a reason to a long, drawn out "If, elseif" clause to cover the different scenarios. 应该没有理由拖出冗长的“ If,elseif”子句来涵盖不同的情况。

To check if any code ran, here is one way. 要检查是否有任何代码运行,这是一种方法。 There are more elegant ways, but this should get you started and be easy enough for you to see what's going on and extend a bit if you need to. 有更多优雅的方法,但是这应该可以帮助您入门,并且足够简单,以便您查看发生了什么并在需要时进行扩展。 As you gain more experience with VBA, you'll undoubtedly refactor this to be more graceful. 随着您获得有关VBA的更多经验,毫无疑问,您将对其进行重构以使其更加优雅。

Sub Test()
Dim AtLeastOneRan As Boolean

    If ActiveSheet.CB("CB1").Value = 1 Then
        AtLeastOneRan = True
        Call M1
    End If

    If ActiveSheet.CB("CB2").Value = 1 Then
        AtLeastOneRan = True
        Call M2
    End If

    If ActiveSheet.CB("CB3").Value = 1 Then
        AtLeastOneRan = True
        Call M3
    End If

    If ActiveSheet.CB("CB4").Value = 1 Then
        AtLeastOneRan = True
        Call M4
    End If

    If ActiveSheet.CB("CB5").Value = 1 Then
        AtLeastOneRan = True
        Call M5
    End If

    If ActiveSheet.CB("CB6").Value = 1 Then
        AtLeastOneRan = True
        Call M6
    End If
    If Not AtLeastOneRan Then MsgBox "Please select at least one option to proceed."
End Sub

If the checkbox name and the sub name are related 1:1, there is a much better way then creating so many IF conditions. 如果复选框名称和子名称为1:1关联,那么有比创建这么多IF条件更好的方法。

Consider these checkboxes named beginning with "CB": 考虑以下以“ CB”开头的复选框:
复选框

With codes in Module1 : 使用Module1中的代码:

Option Explicit

Private Const PREFIX As String = "Module1.M" ' <-- Change this to match your Module Name and Prefix of the Sub Names

Sub LoopCheckboxes()
    Dim sRun As String, oChkBox As Object
    For Each oChkBox In ActiveSheet.CheckBoxes
        With oChkBox
            Debug.Print "Checkbox name: " & .Name
            If .Value = 1 Then
                sRun = PREFIX & Mid(.Name, 3)
                Debug.Print "sRun: " & sRun
                Application.Run sRun
            End If
        End With
    Next
End Sub

Sub M1()
    Debug.Print "M1()"
End Sub

Sub M2()
    Debug.Print "M2()"
End Sub

Sub M3()
    Debug.Print "M3()"
End Sub

When you execute the LoopCheckBoxes , you get: 当执行LoopCheckBoxes时 ,您将获得:
输出量

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

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