简体   繁体   中英

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:

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 !

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:

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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