【问题标题】:userform loop for hiding and unhiding through multiple sequences用于通过多个序列隐藏和取消隐藏的用户表单循环
【发布时间】:2019-01-01 18:23:19
【问题描述】:

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

私有子 CHECK1_Click()

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

它完美地适用于一个复选框和组合框对,我需要它单独处理它们中的所有 61 个...作为新手,我查看了案例选择的可能性,但看起来我必须拼写进出。

用户表单名为“ORDER1”

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

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

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

如何在不将 61 个“点击”事件放入代码的情况下完成这项工作? 哦,我正在使用 excel 2010

【问题讨论】:

  • 最近的类似问题 - stackoverflow.com/questions/51390719/…“控制数组”是您要搜索的术语。顺便说一句,这里最好避免使用全部大写的文本 - 在线通常用于 SHOUTING。
  • 我会调查它...这就是我在目标名称中使用的情况。不要大喊大叫,我很抱歉。
  • 我的意思是你的问题标题...
  • 哎呀...“掌心”
  • 所以这可能是一种愚蠢的做法,但我拿了那串代码并进入电子表格并打破了我需要结合 excel 自动计数和 =CONCATENATE 函数的地方。 ..

标签: excel vba checkbox combobox userform


【解决方案1】:

在 cmets 中已经有一个带有 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多