UserForm Object Arrays

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
Is there a way to set the property of multiple UserForm objects at once. I'm thinking an array but not to sure how to set it up. Below is an example of the code I think could be shortened up using an array or something:

Code:
btnTest.Visible = False
lblTest.Visible = False
txtTest.Visible = False
Anyone know how to do this?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Is it always going to be the same controls or will it vary from time to time?
In the first case, a fixed array (or function, of no arguments, that returns an array) would be a good approach.

This sub may be useful, it will either accept an un-specified number of controls or a (single) array of controls as the argument.

The CommandButton code shows how one would call the ControlVisiblity sub.

Code:
Sub ControlVisibility(Vizable As Boolean, ParamArray myControls() As Variant)
    Dim inputControls As Variant
    Dim oneControl As Variant

    Rem was an array passed as argument
    inputControls = myControls
    If UBound(inputControls) = 0 And TypeName(inputControls(0)) Like "*()" Then
        inputControls = myControls(0)
    End If

    For Each oneControl In inputControls
        oneControl.Visible = Vizable
    Next oneControl
End Sub

Private Sub CommandButton1_Click()
    Call ControlVisibility(False, TextBox1, ListBox1, CheckBox3, CheckBox1)
End Sub

Private Sub CommandButton2_Click()
    Dim arrayOfControls
    arrayOfControls = Array(ListBox1, CheckBox1, CheckBox2)
    
    Call ControlVisibility(Not (ListBox1.Visible), arrayOfControls)
End Sub
 
Upvote 0
The objects to change the visible property will always stay the same but there are 15 or so different controls in the same userform that need a slightly different variation of objects to set the visible property. I was just looking for a cleaner (Shorter) way to write the necessary code.

Maybe something like this:
(Just a thought....i have no idea if it's valid)

Code:
Dim SetVisible

SetVisible = Array(txtTest, lblTest, btnTest)

SetVisible.Visible = False

Would this even work?
 
Upvote 0
No it wouldn't but, with that sub, this would

Code:
Dim SetVisible

SetVisible = Array(txtTest, lblTest, btnTest)

Call ControlVisibility(False, SetVisible)
 
Upvote 0
Ok that makes since. I can use your script to control arrays inside multiple controls. I'll have to give it a shot.

Your script will work for all UserForm objects?
 
Upvote 0
Austin

What are you trying to do here?

Why do you want to have arrays of userforms controls?

You can probably set up an array of controls for a specific userform but I don't know how you would be able to use that array for other other userforms.

Perhaps I'm missing something here.:)
 
Upvote 0
another way to achieve this outcome, which i use sometimes, along with mikerickson's is to collect the controls which will be visible/invisible and put them into a frame. then use Frame1.visible to control all of them in 1 line. you can have 2 frames on top of each other and the user doesnt see that the frame has gone.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top