vba 循环浏览我的 excel 页面上的一些复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13403635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:31:30  来源:igfitidea点击:

Cycling through some checkboxes on my excel page

vba

提问by RoryG

I am VERY new to VBA (and know of it only within excel).

我对 VBA 非常陌生(并且只在 excel 中知道它)。

I am trying to cycle through some (but not all) checkboxes. They are currently named CheckBox1 through CheckBox15. Howe do I cycle through say for instance CheckBox5 through CheckBox10?

我试图循环浏览一些(但不是全部)复选框。它们当前被命名为 CheckBox1 到 CheckBox15。我如何循环,例如 CheckBox5 到 CheckBox10?

I guess I am hoping there is a 'method' similar to 'CheckType' for controls that will allow me to check name?

我想我希望有一种类似于“CheckType”的“方法”可以让我检查名称?

Here is what I have tried. Causes a Compile Error - Sub or Function not defined, and highlights Worksheet.

这是我尝试过的。导致编译错误 - 未定义子或函数,并突出显示工作表。

Private Sub BoxCheck()
atLeastOneChecked = False
    For i = 2 To 4
        If Worksheets("ActiveX").Controls("Checkbox" & i).Value = True Then
            atLeastOneChecked = True
        End If
    Next i
End Sub

While the above doesnt work, what is below does:

虽然以上不起作用,但下面的方法是:

Private Sub BoxCheck()
    atLeastOneChecked = False

    For i = 1 To 2
       If Sheet2.CheckBox2.Value = True Then
                atLeastOneChecked = True
       End If
    Next i

End Sub

Of course, the loop has no affect on the outcome, but it compiles and atLeastOneChecked turns from False to True when Checkbox2 is True. Note that Sheet2 has been named ActiveX. I clearly don't understand how Worksheet and Controls work. Could someone help?

当然,循环对结果没有影响,但是当 Checkbox2 为 True 时,它​​会编译并且 atLeastOneChecked 从 False 变为 True。请注意,Sheet2 已被命名为 ActiveX。我显然不明白 Worksheet 和 Controls 是如何工作的。有人可以帮忙吗?

After fixing the error described below, this still won't work. I simplified to the following:

修复下面描述的错误后,这仍然不起作用。我简化为以下内容:

Private Sub BoxCheck()
    Dim ole As OLEObject

    atLeastOneChecked = False
    Set ole = Sheets("ActiveX").OLEObjects("Checkbox2")
    If ole.Value = True Then
        atLeastOneChecked = True
    End If
 End Sub

This doesn't work. It fails at:

这不起作用。它失败于:

If ole.Value = True Then

The error states: Object Doesn't support this property or method

错误状态:对象不支持此属性或方法

This is true for OLEObjects, but not for Checkboxes. When I look at the properties of ole, I see that its Object property is set to Object/Checkbox and that this Object has a value. I guess that is what I should be referencing in my if statement, but I don't know how.

对于 OLEObjects 是这样,但对于复选框则不然。当我查看 ole 的属性时,我看到它的 Object 属性设置为 Object/Checkbox 并且这个 Object 有一个值。我想这就是我应该在 if 语句中引用的内容,但我不知道如何引用。

回答by RoryG

I think I solved the problem.

我想我解决了这个问题。

The value of the Checkbox is accessed by Referencing the Object property within the OLEObject I set...Like this:

Checkbox 的值是通过在我设置的 OLEObject 中引用 Object 属性来访问的......像这样:

If ole.Object.Value = True Then

Thanks for all your help. If someone has a more elegant solution, I would still like to see it.

感谢你的帮助。如果有人有更优雅的解决方案,我仍然希望看到它。

回答by Parker

Use CheckBox.Name

CheckBox.Name

Example:

例子:

For Each cb In ActiveSheet.CheckBoxes 
    If cb.Name = "CheckBox5"
        ' Do stuff
    End If
Next cb 

回答by Kevin Pope

To expand on @Parker's answer:

扩展@Parker的回答:

Private Sub BoxCheck()
    atleastonechecked = False
    Dim oles As OLEObject
    For i = 2 To 4
        'If you're using Shapes Controls:
        If ThisWorkbook.Worksheets("ActiveX").Shapes("Check Box " & i).Value = True Then
            atleastonechecked = True
        End If
        ' If you're using ActiveX Controls
        Set oles = ThisWorkbook.Worksheets("ActiveX").OLEObjects("CheckBox" & i)
        If oles.Value = True Then
            atleastonechecked = True
        End If
    Next i
End Sub

Sorry, just put together the previous answer without testing - that always fails. Just use the Ifloop based on what type of control you're using.

抱歉,只需将先前的答案放在一起而不进行测试 - 总是失败。只需If根据您使用的控件类型使用循环。