在操作和重新添加项目后重新选择listbox userform中的项目

时间:2017-04-29 00:39:34

标签: excel-vba userform listboxitem vba excel

第一位代码对命令进行调整,然后使用列表框中显示的新顺序刷新列表。

列表框当前设置为" Single",但我希望它被建立为" fmMultiSelectMulti"。从" Multi",我能够使用公共函数调用GetSelectedIndexes(userformNameHere)在字符串中收集所选索引号。 (即" 0,4,7,8,9 ......")

我的问题是,一旦我剪切并粘贴某些行以使用列表框,我希望能够向用户显示他们在所选列表框中仍然具有相同的值。

Public Function GetSelectedIndexes(lBox As MSForms.ListBox) As String
'returns an array of selected index numbers in a listbox
Dim tmparray() As Variant
Dim i As Integer
Dim selCount As Integer
selCount = -1
'## Iterate over each item in the ListBox control:
For i = 0 To lBox.ListCount - 1
    '## Check to see if this item is selected:
    If lBox.Selected(i) = True Then
        '## If this item is selected, then add it to the array
        selCount = selCount + 1
        ReDim Preserve tmparray(selCount)
        tmparray(selCount) = lBox.ListIndex
    End If
Next

If selCount = -1 Then
    '## If no items were selected, return an empty string
    GetSelectedIndexes = "" ' or "No items selected", etc.
Else:
    '## Otherwise, return the array of items as a string,
    '   delimited by commas
    GetSelectedIndexes = Join(tmparray, ", ")
End If
End Function

这是我的其余代码,它与单值可选列表框一起使用:

Private Sub SpinButton1_SpinUp()
' cuts + moves UP one cell...     ONLY WORKS IF FmMultiSelectSingle... 
ThisWorkbook.Sheets("mon").Activate

If Not MonMissions2.ListIndex < 1 Then
    selRow = MonMissions2.ListIndex + 2
    Range("B" & selRow).EntireRow.Select
    Selection.Cut
    Selection.Offset(-1, 0).Insert Shift:=xlDown

'Reloads wing priorities list
MonMissions2.Clear
With MonMissions2
    List = Range("A2:A500").Value
        For i = 1 To UBound(List, 1)
            If Len(Trim(List(i, 1))) > 0 Then
                .AddItem Range("B" & i + 1).Value & "-" & Range("C" & i + 1).Value & "-" & Range("D" & i + 1).Value  ' populate the listbox
            End If
        Next i



    MonMissions2.ListIndex = selRow - 3
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Need to re-select previously selected ListIndex HERE
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End With

End If

End Sub

我是否必须为我的动态列表框项目创建100多个变量,以确定它们是否被选中以开始? (listbox.selected(I)= true ...)

0 个答案:

没有答案