PDA

View Full Version : userform - help needed



Elane
03-04-2024, 02:42 AM
Hello to everyone.
I created a userform (UserForm1), and inside this form I placed 20 textboxes and 20 checkboxes.
Everytime I make the userform to appear, I need all the checkboxes to be false and all the listboxes to be empty
One question at a time...
I can write down 20 rows to set each checkbox value=false but I would like to have something better
I tried

For i = 1 To 20
UserForm1.Controls("CheckBox" & Chr(64 + i)).Value = False
Next i
but it says runtime error, impossible to find the specified object
These instructions are placed in the command button macro, just before the UserForm1.show line

How can I fix the error?
Thanks to everyone

georgiboy
03-04-2024, 02:50 AM
Hi Elane, welcome to the forum.

Try as below:

Dim i As Integer

For i = 1 To 3
UserForm1.Controls("CheckBox" & i).Value = False
Next i
UserForm1.Show

Unless your checkbox's are named 'CheckBoxA', 'CheckBoxB' etc.?

arnelgp
03-04-2024, 06:38 AM
you may also try this on the Activate event of your UserForm


Private Sub UserForm_Activate()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
ctl = ""
ElseIf TypeOf ctl Is CheckBox Then
ctl = 0
End If
Next
End Sub