简体   繁体   English

VBA - 如何动态访问用户表单

[英]VBA - how to dynamically access userform

do you know a method to access userform which name is stored in a variable?你知道一种访问用户表单的方法,哪个名称存储在变量中? I have class module with events for checkboxes, but checkboxes are located on different userforms and I need to operate with them or theirs userforms.我有带有复选框事件的类模块,但复选框位于不同的用户表单上,我需要使用它们或他们的用户表单进行操作。

Class module have its public variable chbParent, which is userform name.类模块有它的公共变量 chbParent,它是用户表单名称。 Lets say that让我们说

chParent = "formCmnStructs"

Then inside of class module I have this (and more operations where condition is required):然后在类模块内部,我有这个(以及需要条件的更多操作):

If chbParent = "formInsertRow" Then
  Divisions(r, c).Checked = IIf(formInsertRow.Controls("chbDiv_" & Me.chbId).value = True, 1, 0)
  formInsertRow.chbDivClicked
ElseIf chbParent = "formCmnStructs" Then
  Divisions(r, c).Checked = IIf(formCmnStructs.Controls("chbDiv_" & Me.chbId).value = True, 1, 0)
  formCmnStructs.chbDivClicked
End If

Now it is used for two forms only, but more is comming.现在它仅用于两种形式,但更多形式即将到来。 Is there any way to cancel condition and acces userform dynamically like this:有没有办法像这样取消条件并动态访问用户表单:

userforms(chbParent).Controls("....

You could use public variable like ActiveForm as object and for each userform您可以使用像ActiveForm as object这样的公共变量ActiveForm as object和每个用户表单

Private Sub UserForm_Activate()

    Set ActiveForm = Me

End Sub

And then in class module ActiveForm.Controls("...然后在类模块ActiveForm.Controls("...
To get the form name chParent = ActiveForm.Name获取表单名称chParent = ActiveForm.Name

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

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