在 VBA 中重置列表框选择 [英] Reset listbox selection in VBA

查看:70
本文介绍了在 VBA 中重置列表框选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当表单关闭时,我正在尝试在 Excel VBA 中重置"列表框.目前,当我使用 userform1.hide 函数时,表单会消失,但是当我使用 .show 函数再次打开它时,它仍然有之前的选择.作为对此相对较新的人,任何人都可以提供帮助吗?

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 the .show function it still has the previous selections in it. As someone who is relatively new to this can anyone help?

列表框的代码如下:

Sub CommandButton1_Click()

'Filter by Country
Dim item As Long, dict As Object
Dim wsData As Worksheet

Set wsData = Sheets("TPID")
Set dict = CreateObject("Scripting.Dictionary")

With ListBox1
    For item = 0 To .ListCount - 1
        If .Selected(item) Then dict(.List(item)) = Empty
    Next item
End With

With wsData.ListObjects("Table_ExternalData_1").Range
    .AutoFilter Field:=1
    If dict.Count Then _
        .AutoFilter Field:=1, criteria1:=dict.keys, Operator:=xlFilterValues
End With
'Filter by Continent
Dim item1 As Long, dict1 As Object
Dim wsData1 As Worksheet

Set wsData1 = Sheets("TPID")
Set dict1 = CreateObject("Scripting.Dictionary")

With ListBox2
    For item1 = 0 To .ListCount - 1
        If .Selected(item1) Then dict1(.List(item1)) = Empty
    Next item1
End With

With wsData1.ListObjects("Table_ExternalData_1").Range
    .AutoFilter Field:=4
    If dict1.Count Then _
        .AutoFilter Field:=4, criteria1:=dict1.keys, Operator:=xlFilterValues
End With


End Sub

提前谢谢大家,

推荐答案

如果您只想清除选择(因为您使用的是隐藏,而不是卸载)然后使用:

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

这篇关于在 VBA 中重置列表框选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆