Excel VBA表单控件 - 重置列表框滚动条

时间:2014-08-01 00:39:31

标签: excel vba listbox scroll position

我编写了下面的代码来从同一张纸上的六个列表框(多选)中提取数据,将选择传递给模块进行计算,然后清除列表框选择。唯一一个看起来微不足道的问题,虽然非常令人沮丧的是让每个列表框的列表框“滚动条”将其位置重置为列表的顶部。

我试过.TopIndex但是因为我使用的是表单控件而不是ActiveX控件,所以它返回不支持的“对象”。

有谁知道如何将列表框滚动条位置重置到表单控件列表框的顶部?

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    

2 个答案:

答案 0 :(得分:1)

我发现它是清除和重新插入值的唯一方法......

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

因为 ListIndex 选择了第一个元素,但没有激活,你无法使用键盘移动到内部...

答案 1 :(得分:0)

我不是专家写一个代码来帮助你,但我有一个想法,你可以尝试编码或其他比我们可以帮助更聪明的人。

您说您正在使用多重选择,因此在重置时,您是否可以执行以下操作?:

  1. 删除所有选择
  2. 关闭多重选择以单选
  3. 选择列表框中的第一项(即将焦点置于顶部)
  4. 删除此选择
  5. 将此列表框返回到“多选”
  6. 我想尝试单一选择的原因是因为多选择列表框可以选择而不关注所选项目。这更像是一种理论,但看到没有人尝试过我认为我会尝试帮助的答案。任何人都可以确认或否认我的想法吗?