PDA

View Full Version : [SOLVED:] Userform is not displaying anything when i change a "box" to visible or not visible



Doug2009
08-06-2022, 12:31 PM
The fields get changed, but when I UserForm1.Show, the form comes up blank. I want to be able to show or hide items in the form as needed.
I can get it to work if i explicitly type in the code. ie Userform1.Listbox1.Visible = False
For Multipage ... it will select the proper page and display or not as the program chooses, but that is as far as it goes.

What am I doing wrong?



For Each objControl In UserForm1.Controls
'Sheets("Controls").Columns(1) is a list of items i want to be visible, the rest should be hidden


iVisibleFieldsColumn = 0
iVisibleFieldsColumn = Application.Match(objControl.Name, Sheets("Controls").Columns(1), 0)
If iVisibleFieldsColumn > 0 Then
objControl.Visible = True
Else
objControl.Visible = False
End If
End If
Next objControl

arnelgp
08-06-2022, 05:47 PM
as far as your code is concern, if initially they are Set (True) in the sheet, they will
be all set to False.

maybe you add an Optional "control" name as a parameter to the sub/func so you can
explicitly turn it on/off.

p45cal
08-07-2022, 12:10 AM
Best attach a workbook with only the user form and associated code still misbehaving. Then we can be sure when your code runs and how you've set up your user form at design time. Otherwise we're just guessing, and probably guessing wrongly.

Doug2009
08-07-2022, 12:14 AM
Thank you for your comments.

They are all set true and are reset true or false as my list dictates. The list represents all I wish true (visible) and includes listboxes, textboxes, and command buttons. I have actually tried setting them all false, and that did not change the end result with is the form comes up, but is completely blank. Again, i checked to see if the visible fields get changed, and they do.

Doug2009
08-07-2022, 12:27 AM
There are two macros. The one shows the modified userform is: VisibleSwitch the other DisplayFormControls shows all the objects and holds my list of items I wish on, with the others off.

SamT
08-07-2022, 02:44 AM
My way
Dim objControl as Object
Dim i As Long
Dim Arr

For each objControl in Userform.Controls
objControl.Visible = False
Next

'Use array for speed
Arr = Intersect(Sheets("Controls").UsedRange, Range("A:A")).Value
For 1 = Lbound(Arr, 2) to Ubound(Arr, 2)
'Use Error for typos, etc.
On Error Resume Next
UserForm.Controls(Arr(1, i)).Visible = True
Error = 0
Next

Your way

For Each objControl In UserForm1.Controls
objControl.Visible = Not(Application.Match(objControl.Name, Sheets("Controls").Columns(1), 0) = "#N/A")
Next objControl

snb
08-07-2022, 03:32 AM
I think your approach is not correct: don't use a worksheet to determine whether a control in a Userform should be visible or not,
In Design Mode you should decide which controls should be visible when showing the Userform for the first time (Userform_Initialize).
If anything should be changed after that you should use the Userform's eventprocedures to adapt the lay-out/visibility in the Userform.

Your initial question (though based on a wrong concept) could be answered this way:


Private Sub UserForm_Initialize()
sn = Sheet1.Cells(1).CurrentRegion

For j = 3 To UBound(sn)
Me(sn(j, 1)).Visible = sn(j, 3)
Next
End Sub

arnelgp
08-07-2022, 03:58 AM
here is what i have.
i added another sheet and add Table.
see the Initialize event of the Userform.

Doug2009
08-07-2022, 05:24 AM
Thanks to all of you!

You have no idea how long I struggled with this! Your solutions are simple and elegant! Thank you very much!