Excel VBA error in filling and not filling a list

I want to populate the list box when the check mark is ticked and empty when the check mark is removed.

This code worked for this function in my previous modules, but now I am getting an error (I assume these are arguments for Range) and I would like to understand why. In addition, the list box remains the same as when unchecked.

Here is my code:

Private Sub CheckBox1_Click()
    If Me.CheckBox1.Value = True Then
        ListBox1.List = Sheets("DATA").Range("C22").Value
    Else
        ListBox1.ListFillRange = ""
    End If
End Sub

      

+3


source to share


2 answers


Try the following:



Private Sub CheckBox1_Click()
    If Me.CheckBox1.Value = True Then
        Me.ListBox1.AddItem Sheets("DATA").Range("C22").Value
    Else
        Me.ListBox1.Clear
    End If
End Sub

      

0


source


Change the code as follows:

Private Sub CheckBox1_Click()
If Me.CheckBox1.Value = True Then
    ListBox1.ListFillRange = "C22:C24"
Else
    ListBox1.ListFillRange = ""
End If
End Sub

      



will modify the list to display the contents of cells C22: C24.

0


source







All Articles