在 VBA 中重置列表框选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18654252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reset listbox selection in VBA
提问by user2753700
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?
当表单关闭时,我试图在 Excel VBA 中“重置”列表框。目前,当我使用 userform1.hide 函数时,表单消失了,但是当我使用 .show 函数再次打开它时,它仍然包含以前的选择。作为对此比较陌生的人,有人可以提供帮助吗?
The code for the listboxes is as follows:
列表框的代码如下:
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
Thanks in advance everyone,
提前谢谢大家,
回答by SilverShotBee
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
回答by user28864
try this code to Clear listbox in VBA
尝试使用此代码清除 VBA 中的列表框
Private Sub clearListBox()
Dim iCount As Integer
For iCount = 0 To Me!ListBox1.ListCount
Me!ListBox1.Selected(iCount) = False
Next iCount
End Sub
回答by InHoaxidable
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.
之后列表框控件中将没有明显的项目。
回答by BenJr
You could use
你可以用
Private Sub clearListBox()
'Clears the listbox
Do Until ListBox1.ListCount = 0
Me!ListBox1.RemoveItem(0)
Loop
End Sub
回答by Thomas
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
另一种做你想做的事情的方法是简单地浏览所有单选按钮并取消选中它们