简体   繁体   English

在VBA中确定Control X复选框名称

[英]Determining Control X checkbox Name in VBA

Can someone tell me what the syntax is to determine the controlX checkbox name? 有人可以告诉我确定controlX复选框名称的语法是什么? I have approximately 4 check boxes and this may potentially grow, so I'd like a method of passing through the checkbox name dynamically rather than writing the same execution 4-9 times. 我大约有4个复选框,并且这个数目可能会增长,所以我想要一种动态传递复选框名称的方法,而不是将相同的执行写入4-9次。

My intention is to pass through the checkbox name as a variable so I do not have to repeat the below code for each checkbox. 我的意图是将复选框名称作为变量传递,因此我不必为每个复选框重复以下代码。 Also, does anyone know how to reference a named range to a specific checkbox? 另外,有人知道如何将命名范围引用到特定复选框吗? The code I have so far is: 到目前为止,我的代码是:

Sub CheckBox1_Click()

Application.ScreenUpdating = False

Dim strCheck As String
strCheck = CheckBox1.Value

If strCheck = True Then

    Range("RevAssp_CCV").Select
    Selection.EntireRow.Hidden = False

   Else
    Range("RevAssp_CCV").Select
    Selection.EntireRow.Hidden = True


End If

End Sub

Thanks in advance 提前致谢

The easiest way would be to create a custom wrapper class, create an array of objects of said class and then hook into the event there. 最简单的方法是创建一个自定义包装类,创建该类的对象数组,然后在其中挂接事件。 You can then (for example) check the Caption and set the "hidden" of the NamedRange.EntireRow equal to the value (eg checked is invisible, unchecked is visible) 然后,您可以(例如)检查Caption并将NamedRange.EntireRow的“ hidden”设置为等于值(例如,checked是不可见的,unchecked是可见的)

The most basic implementation of this would be as follows: 最基本的实现如下:

CustomCheckBox Class module: CustomCheckBox类模块:

Private WithEvents p_chkBox As MSForms.checkbox
Public Property Let box(value As MSForms.checkbox)
    Set p_chkBox = value
End Property
Public Property Get box() As MSForms.checkbox
    Set box = p_chkBox
End Property
Private Sub p_chkBox_Click()
    Range(p_chkBox.Caption).EntireRow.Hidden = p_chkBox.value
End Sub

And in a regular module: 并在常规模块中:

Public cCheckBox() As CustomCheckBox
Sub Test()
Dim ws As Worksheet
Dim oleObj As OLEObject
Dim i As Integer
i = 0
For Each ws In ThisWorkbook.Worksheets
    For Each oleObj In ws.OLEObjects
        If TypeName(oleObj.Object) = "CheckBox" Then
            ReDim Preserve cCheckBox(0 To i)
            Set cCheckBox(i) = New CustomCheckBox
            cCheckBox(i).box = oleObj.Object
            i = i + 1
        End If
    Next oleObj
Next ws
End Sub

The regular module puts all checkboxes into 1 array, which is a public variable so it will be available even after the code has run. 常规模块将所有复选框放入1个数组中,这是一个公共变量,因此即使在代码运行后它也将可用。 You could also place this code in the Workbook Module as Private Sub Workbook_Open to ensure that the checkboxes will be initialized properly in all cases. 您还可以将此代码作为Private Sub Workbook_Open放置在工作簿模块中,以确保在所有情况下都可以正确初始化复选框。

Keep in mind that if the Named Range for the caption of the Checkbox doesn't exist, this will throw errors. 请记住,如果不存在复选框标题的命名范围,则将引发错误。

To get back to your example, you could now just add two checkboxes on your sheets and set the caption of the first one to "RevAssp_CCV" and the second one to whatever other named range you wish to toggle. 回到您的示例,您现在可以在工作表上添加两个复选框,并将第一个复选框的标题设置为“ RevAssp_CCV”,将第二个标题设置为您希望切换的任何其他命名范围。

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

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