Consulting

Results 1 to 3 of 3

Thread: userform - help needed

  1. #1
    VBAX Newbie
    Joined
    Feb 2024
    Posts
    1
    Location

    userform - help needed

    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

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    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.?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •