none
Select the last item in a listbox with multiselect activated Excel 2010 RRS feed

  • Question

  • Hello guys,

    I would like to select the last item in a listbox, but it is a listbox with multiselect activated. I really stuck know.

    This is my code so far:

    Private Sub UserForm_Initialize()
    ListBox1.MultiSelect = 2
    ListBox2.MultiSelect = 2

    Dim RowsNumber As Integer
    Dim RowsNumberOnly As Integer
    Dim i As Integer
    Dim Customers As Long
    Dim customersloop As Long
    Dim test As String
    RowsNumber = FunctionCount.calculateRows
    RowsNumberOnly = FunctionCount.calculateRowsValue

    ListBox1.ColumnCount = 3
    ListBox1.ColumnWidths = "70;50;50"

    ListBox2.ColumnCount = 3
    ListBox2.ColumnWidths = "70;50;50"


        Customers = 10
        i = 0

          For customersloop = Customers To RowsNumber
                ListBox1.AddItem
                'ListBox1.List(i, 0) = Sheets("Test").Range("J" & customersloop).Value   & "       " & Sheets("Test").Range("K" & customersloop).Value & "     " & Sheets("Test").Range("L" & customersloop).Value
                ListBox1.List(i, 0) = Sheets("Test").Range("J" & customersloop).Value
                ListBox1.List(i, 1) = Sheets("Test").Range("K" & customersloop).Value
                ListBox1.List(i, 2) = Sheets("Test").Range("L" & customersloop).Value
                i = i + 1
          Next

    End Sub

    thanks in advance!

    Matthias

    Tuesday, March 22, 2016 11:43 AM

Answers

  • Private Sub UserForm_Initialize()
      Dim Data
      
      With Sheets("Test")
        'Read all data into an array
        Data = .Range("J10", .Range("L" & .Rows.Count).End(xlUp)).Value
      End With
      
      With ListBox1
        'Store into the listbox
        .List = Data
        'Customize some settings
        .MultiSelect = fmMultiSelectExtended
        .ColumnCount = 3
        .ColumnWidths = "70;50;50"
        'Select last item
        .Selected(.ListCount - 1) = True
      End With
      
      With ListBox2
        .MultiSelect = fmMultiSelectExtended
        .ColumnCount = 3
        .ColumnWidths = "70;50;50"
      End With
    End Sub



    Tuesday, March 22, 2016 4:40 PM