Excel VBA表单控件-重置列表框滚动条
我编写了下面的代码,从同一张表上的六个列表框(multiselect)中提取数据,将选择传递给一个模块进行计算,然后清除列表框选择。唯一的问题似乎可以忽略,尽管非常令人沮丧,是让每个列表框的列表框“滚动条”将其位置重置为列表顶部 我尝试过.TopIndex,但因为我使用的是表单控件而不是ActiveX控件,所以它返回的“object”不受支持 有人知道如何将列表框滚动条位置重置为表单控件列表框的顶部吗Excel VBA表单控件-重置列表框滚动条,excel,vba,listbox,scroll,position,Excel,Vba,Listbox,Scroll,Position,我编写了下面的代码,从同一张表上的六个列表框(multiselect)中提取数据,将选择传递给一个模块进行计算,然后清除列表框选择。唯一的问题似乎可以忽略,尽管非常令人沮丧,是让每个列表框的列表框“滚动条”将其位置重置为列表顶部 我尝试过.TopIndex,但因为我使用的是表单控件而不是ActiveX控件,所以它返回的“object”不受支持 有人知道如何将列表框滚动条位置重置为表单控件列表框的顶部吗 Sub Listboxproperties_click() 'store selec
Sub Listboxproperties_click()
'store selected items from listbox into an array
Dim listarray()
Dim J As Integer
Dim R As Integer
Dim i As Integer
'Add selected items into the array
ReDim listarray(1 To 50, 1 To 6)
'Counter
J = 0
For R = 1 To 6
Set lb = ActiveSheet.ListBoxes("ListBox" & R)
For i = 1 To lb.ListCount
If lb.Selected(i) = True Then
'add 1 to the counter
J = J + 1
'Store selection in an array
listarray(J, R) = lb.list(i)
End If
Next i
J = 0
Next R
'Check if msgbox has a selection if not exit sub
For R = 1 To 6
'if there is nothing in the first item of the listarray then the user has not chosen an option
If listarray(1, R) = "" Then
MsgBox "You have not selected a option, please select and retry"
Exit Sub
End If
Next R
'input box for the name of the Trend
Linename = InputBox("Please enter a name for the call type you are calculating i.e. Adviser Calls, Withdrawal Status etc", "Call Trend")
If Linename = "" Then
MsgBox "No name selected, please retry and enter a name for your call flow"
Exit Sub
End If
Call UniqueCount(listarray, Linename)
'clear selections from listbox
For R = 1 To 6
Set lb = ActiveSheet.ListBoxes("ListBox" & R)
For i = 1 To lb.ListCount - 1
If lb.Selected(i) = True Then
lb.Selected = False
End If
Next i
lb.TopIndex
Next R
End Sub
我没有足够的专家来编写代码来帮助你,但我有一个想法,那就是你可以尝试编写代码,或者其他比我们更聪明的人可以帮助你 您说您正在使用multi-select,因此在重置时,您可能会执行以下操作吗
我之所以想尝试单选,是因为使用“多选”时,列表框可以选择,而不必关注所选项目。这更像是一种理论,但鉴于没有人试图给出答案,我想我会尽力帮忙。有人能证实或否定我的想法吗?我发现的唯一方法是清除并重新插入值
Dim xx(1 To 10000) As String
Set o = ActiveSheet.Shapes("List Box 1")
e = 1
For e = 1 To o.ControlFormat.ListCount
xx(e) = o.ControlFormat.List(e)
Next
o.ControlFormat.RemoveAllItems
For i = 1 To e
o.ControlFormat.AddItem xx(i)
Next
o.ControlFormat.ListIndex = 1
因为列表索引选择了第一个元素,但不是活动的,并且您不能使用键盘在其中移动