简体   繁体   English

如何从 vba 中的控件名称生成控件?

[英]How to generate control from control name in vba?

I have a sub that recive a control name in argument as string and I want to convert the name to a control I tried this sub:我有一个 sub 将参数中的控件名称作为字符串接收,我想将该名称转换为我尝试过的控件:

Public Sub text_initialize(ByVal CurrentPage As String)
    Dim ctrl As Control
    For Each ctrl In UserForm1.MultiPage1.Controls(CurrentPage).Controls
        'some code here
    Next ctrl
End Sub

and in the call I did this:在通话中我这样做了:

text_initialize "Page1"

I found that solution "Controls(CurrentPage)" mentioned for textboxs but it dosen't seem to works for pages !我发现为文本框提到的解决方案“Controls(CurrentPage)”,但它似乎不适用于页面!

And if there is any other way to pass page as argument so that I can modify all controls on it I would like to know.如果有任何其他方法可以将页面作为参数传递,以便我可以修改它上面的所有控件,我想知道。

Thank's to @CherryDT he found two mistakes in my code the first that I should use Page1 instead of page1 and Pages instead of Controls and I found out that for some reason I had to replace MultiPage1 by UserForm1.MultiPage1 so the correct code now is:感谢@CherryDT,他在我的代码中发现了两个错误,首先我应该使用Page1而不是page1Pages而不是Controls ,我发现由于某种原因我不得不用UserForm1.MultiPage1替换MultiPage1所以现在正确的代码是:

Public Sub text_initialize(ByVal CurrentPage As String)
    Dim ctrl As Control
    For Each ctrl In UserForm1.MultiPage1.Controls(CurrentPage).Controls
        'some code here
    Next ctrl
End Sub

And call it like that:并这样称呼它:

text_initialize "Page1"

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

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