简体   繁体   中英

Excel VBA Error in Listbox populating and unpopulating

I am looking to populate a list box when a check box is ticked and empty it when the tick is removed.

This code worked for this function in my previous modules but now I am getting an error (im guessing it is with the arguments for Range), and I would like to understand why. Additionally, the list box remains as it is when the checkbox is unticked.

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

Try this:

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

Changing your code to this:

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

will change the listbox to show the contents of cells C22:C24.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM