简体   繁体   English

在 VBA excel 中控制用户窗体上动态创建的控件

[英]Controlling dynamically created controls on a userform in VBA excel

I have created a multipage user form which dynamically populates with a set of identical frames and each of them has 2 option buttons based on previous user selections.我创建了一个多页用户表单,它动态地填充了一组相同的框架,并且每个框架都有 2 个基于先前用户选择的选项按钮。 I am trying to check if at least one of the option buttons is selected within each frame but don't seem to access the option buttons in code even through I know what their names will be.我正在尝试检查是否至少在每一帧中选择了一个选项按钮,但似乎没有访问代码中的选项按钮,即使我知道它们的名称是什么。 I will then be transferring the selection to a worksheet so need to be able to see what they have selected.然后我会将选择转移到工作表中,因此需要能够看到他们选择的内容。 Any help would be appreciated, I use VBA for excel infrequently so its always a struggle to be honest.任何帮助将不胜感激,我很少将 VBA 用于 excel,因此老实说总是很困难。

在此处输入图像描述

I'm getting closer, I've used this code of another post and changed it slightly while I trial what I am doing.我越来越近了,我已经使用了另一篇文章的这段代码并在我尝试我正在做的事情时稍微改变了它。 Getting there slowly.慢慢地到达那里。 :) :)

I'm not sure what some of the Class modules part is doing but its working.我不确定 Class 模块部分在做什么,但它在工作。

Forms: Userform1 Forms:用户表单 1




Option Explicit
 
Friend Sub OptionButtonSngClick(o As MSForms.OptionButton)
Dim cControlCheck As MSForms.Control
Dim cControlCheck1 As MSForms.Control
Dim cControlFrame As MSForms.Control
Dim strName As String


If Left(o.Name, 2) = "qN" Then
    o.BackColor = RGB(256, 0, 0)
ElseIf Left(o.Name, 2) = "qY" Then
    o.BackColor = RGB(0, 256, 0)
End If

For Each cControlCheck In UserForm1.Controls
    If TypeName(cControlCheck) = "Frame" Then
    For Each cControlCheck1 In Me.Controls(cControlCheck.Name).Controls
        If TypeName(cControlCheck1) = "OptionButton" Then
            If cControlCheck1 = False Then
                cControlCheck1.BackColor = RGB(240, 240, 240)
            End If
        End If
    Next
                
    End If
Next
      

End Sub

Friend Sub cmdCheck_Click()
Dim cControlCheck2 As MSForms.Control
Dim cControlCheck3 As MSForms.Control

Dim cCollection As Collection

Set cCollection = New Collection

For Each cControlCheck2 In UserForm1.Controls
    If TypeName(cControlCheck2) = "Frame" Then
    For Each cControlCheck3 In Me.Controls(cControlCheck2.Name).Controls
        If TypeName(cControlCheck3) = "OptionButton" Then
               cCollection.Add cControlCheck3
              
        End If
    Next
                
    End If
Next

If cCollection(1).Value = False And cCollection(2).Value = False Then

MsgBox ("Make a selection")


End If


End Sub

Class Module: OPtionButtonEvents Class 模块:OPtionButtonEvents

Option Explicit
 
  Private WithEvents ob As MSForms.OptionButton
  Private CallBackParent As UserForm1
  Private CallBackParent1 As UserForm1
 
Private Sub ob_Change()

End Sub

    Private Sub ob_Click()
    
    Call CallBackParent.OptionButtonSngClick(ob)
    
    End Sub

  Private Sub ob_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
       Call CallBackParent.OptionButtonDblClick(ob)
  End Sub
 
  Friend Sub WatchControl(oControl As MSForms.OptionButton, oParent As UserForm1)
       Set ob = oControl
       Set CallBackParent = oParent
  End Sub

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

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