Reset listbox selection in VBA

前端 未结 5 475
渐次进展
渐次进展 2021-01-18 18:08

I\'m trying to \'reset\' a listbox in Excel VBA when a form closes. Currently when I use the userform1.hide function the form disappears but when I open it up again using th

相关标签:
5条回答
  • 2021-01-18 18:12

    You could use

    Private Sub clearListBox()
     'Clears the listbox
        Do Until ListBox1.ListCount = 0
            Me!ListBox1.RemoveItem(0)
        Loop
    End Sub
    
    0 讨论(0)
  • 2021-01-18 18:16

    Hide and show has no effect. If you want to use "brute force", use unload then load, but it will reset everything (not just the radio buttons) and will be memory consuming (well, if your form doesn't contain thousands of components and your computer is recent etc etc it will be fine though)

    another way to do what you want is simply to run through all radio buttons and uncheck them all

    0 讨论(0)
  • 2021-01-18 18:22

    If you want to clear ONLY the selection (as you are using hide, not unload) then use:

    me.listbox1.value = ""
    

    If it is a multiselect listbox, you need to use:

    Me.listbox1.MultiSelect = fmMultiSelectSingle
    Me.listbox1.Value = ""
    Me.listbox1.MultiSelect = fmMultiSelectMulti
    

    this will clear the selection by setting it to single selection only and then clearing the selection, then setting the functionality to multi select again.

    If you want to clear the entire list box (the options that you select) use:

    Me.listbox1.clear
    
    0 讨论(0)
  • 2021-01-18 18:34

    To reset the apparent item selected in the listbox, try:

    ListBox1.ListIndex = -1
    ListBox2.ListIndex = -1
    

    There will be no apparent item in the listbox control after that.

    0 讨论(0)
  • 2021-01-18 18:37

    try this code to Clear listbox in VBA

    Private Sub clearListBox()
        Dim iCount As Integer
    
        For iCount = 0 To Me!ListBox1.ListCount
            Me!ListBox1.Selected(iCount) = False
        Next iCount
    End Sub
    
    0 讨论(0)
提交回复
热议问题