Make some CheckBoxes in one Form behavie according to a ListBox of another Form

davileal

New Member
Joined
Jan 25, 2020
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hey, guys.

I put some CheckBoxes in my Form and make all of them invisible. Then I want to make only a few of them visible according to the number of Itens that the 'User' insert on a ListBox from the past Form. I also want to change the captions of these CheckBox to the same name of the itens of the ListBox.

I try this:

VBA Code:
Private Sub UserForm_Activate()
    
            If IsNull(UserForm2.ListBox1.List(0)) Then
                CheckBox1.Visible = False
            Else:
                CheckBox1.Caption = UserForm2.ListBox1.List(0)
            End If
            
            
            If IsNull(UserForm2.ListBox1.List(1)) Then
                CheckBox2.Visible = False
            Else:
                CheckBox2.Caption = UserForm2.ListBox1.List(1)
            End If

            ...
End Sub

The CheckBoxex go until 20. My problem is: If the number of the ListBox itens is 10, I have trouble with the code UserForm2.ListBox1.List(10).

I'm a noob on VBA. Please give me some light. Thanks advance.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try this method
- it uses loops to avoid repeating the code for each checkbox

Two subs are called from UserForm_Activate
- ONE to hide all checkboxes and the OTHER to make some visible with caption matching item in Listbox1 in UserForm2

VBA Code:
Private Sub UserForm_Activate()
    Call HideCheckBoxes
    Call ShowCheckBoxes
End Sub

Private Sub HideCheckBoxes()
    Dim c As Control
    For Each c In Me.Controls
        If TypeName(c) = "CheckBox" Then c.Visible = False
    Next
End Sub

Private Sub ShowCheckBoxes()
    Dim c As Control, x As Long
    With UserForm2.listbox1
        For x = 1 To .ListCount
            For Each c In Me.Controls
                If c.Name = "CheckBox" & x Then
                    c.Visible = True
                    c.Caption = .List(x - 1)
                    Exit For
                End If
            Next c
        Next x
    End With
End Sub

The code should not fail even if the listbox is empty
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,033
Members
448,940
Latest member
mdusw

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