用于隐藏和取消隐藏多个序列的userform循环

时间:2018-07-25 03:19:43

标签: excel-vba loops checkbox combobox userform

所以我有这串代码...它是一种用户形式,并且都是基于vba的(IE不会从电子表格中提取数据。)

私人Sub CHECK1_Click()

If CHECK1.value = False Then
    COMBO1.visible = False
        Else
            COMBO1.visible = True
    End If
End Sub

它完美地适用于恰好一对复选框和组合框,我需要它分别对它们中的全部61个起作用。。。作为新手,我研究了案例选择的可能性,但看起来我必须拼写由内而外。

该用户表单称为“ ORDER1”

所有复选框都通过“ CHECK61”命名为“ CHECK1”

它们都对应于通过“ COMBO61”恰当地命名为“ COMBO1”的组合框

(CHECK1 = COMBO1贯穿整个表格。)

如何在不将61个'click'事件放入代码的情况下进行这项工作? 哦,我正在使用excel 2010

1 个答案:

答案 0 :(得分:0)

在注释中,已经有一个带有WithEvents的“控件数组”作为可能的解决方案,下面,我将显示另一个没有WithEvents的解决方案:

将此代码复制到记事本,并将其另存为CatchEvents.cls

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "CatchEvents"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Private Type GUID
      Data1 As Long
      Data2 As Integer
      Data3 As Integer
      Data4(0 To 7) As Byte
End Type

#If VBA7 And Win64 Then
      Private Declare PtrSafe Function ConnectToConnectionPoint Lib "shlwapi" Alias "#168" (ByVal punk As stdole.IUnknown, _
              ByRef riidEvent As GUID, ByVal fConnect As Long, ByVal punkTarget As stdole.IUnknown, ByRef pdwCookie As Long, _
              Optional ByVal ppcpOut As LongPtr) As Long
#Else
     Private Declare Function ConnectToConnectionPoint Lib "shlwapi" Alias "#168" (ByVal punk As stdole.IUnknown, ByRef riidEvent As GUID, _
              ByVal fConnect As Long, ByVal punkTarget As stdole.IUnknown, ByRef pdwCookie As Long, Optional ByVal ppcpOut As Long) As Long
#End If

'All Other Control-Events also possible
'No need to use WithEvents
'No need to put every Type of control in seperate collection or array, each event will fire no matter which control,
'so in the eventcode controls can be separated from others
'you can give your controls additional properties
'Arguments as Cancel, KeyCode, KeyAscii , Button , x and Y still can be used

Private EventGuide As GUID
Private Ck As Long
Private ctl As Object
Private CustomProp As String

Public Sub MyListClick()
Attribute MyListClick.VB_UserMemId = -610
If TypeName(ctl) = "CheckBox" Then
ctl.Parent.Controls(Replace(ctl.Name, "CHECK", "COMBO")).Visible = ctl.Value
End If
End Sub

Public Sub ConnectAllEvents(ByVal Connect As Boolean)
      With EventGuide
          .Data1 = &H20400
          .Data4(0) = &HC0
          .Data4(7) = &H46
      End With
      ConnectToConnectionPoint Me, EventGuide, Connect, ctl, Ck, 0&
End Sub

Public Property Let Item(Ctrl As Object)
      Set ctl = Ctrl
      Call ConnectAllEvents(True)
End Property

Public Sub Clear()
      If (Ck <> 0) Then Call ConnectAllEvents(False)
      Set ctl = Nothing
End Sub

在VBA编辑器中,右键单击项目的某个位置,然后选择“导入文件”,现在将在您的类模块中添加一个名为CatchEvents的类。

最后将下面的代码粘贴到用户表单的后面:

Private AllControls() As New CatchEvents    

Private Sub UserForm_Initialize()
Dim j As Long
ReDim AllControls(Controls.Count - 1)
    For j = 0 To Controls.Count - 1
         AllControls(j).Item = Controls(j)
    Next
End Sub

Private Sub UserForm_Terminate()
Dim j As Long
  For j = LBound(AllControls) To UBound(AllControls)
          AllControls(j).Clear
      Next j
      Erase AllControls
End Sub