How to add ListIndex into ListBox in VBA Excel

virakbot

New Member
Joined
Aug 13, 2018
Messages
13
Dear All Masters,

I want to make my ListBox to be tidy with ListIndex. It means that I want to have straight line column in ListBox. Currently, I don't know how to input ListIndex into below code. Pls kindly help me accordingly. Thanks all Masters.

Sub Pending()
Dim r As Long
Dim msg As String, i As Long, a As Variant




UserForm1.ListBox1.Clear


For i = 13 To 100000
If Range("F" & i) <> "" And Cells(i, 14).Value = "" Then


UserForm1.ListBox1.AddItem Format(Range("E" & i), "dd-mmm") & " " & Range("F" & i) & " " & Range("G" & i) & " " & Range("K" & i)


End If
Next


UserForm1.Show


End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
What would you use ListIndex for in that code?

ListIndex indicates which item has been selected in the listbox.
 
Upvote 0
Sir !! Sorry, I don’t know exactly about function of ListIndex, and maybe I am wrong to talk about it. Above codes can only provide me a ListBox with untidy data. I want it to be tidy as it is in table, straight column, something like this. thx sir
 
Upvote 0
You are probably referring to the ColumnCount property, which sets the number of columns in a listbox:
Code:
Sub Pending()
    Dim r As Long
    Dim msg As String, i As Long, a As Variant
    With UserForm1.ListBox1
    .Clear
    .ColumnCount = 4
    .ColumnWidths = "25;25;25;15"
    For i = 13 To 100
        If Range("F" & i) <> "" And Cells(i, 14).Value = "" Then
            .AddItem Range("E" & i).Value
            .List(.ListCount - 1, 1) = Range("F" & i).Value
            .List(.ListCount - 1, 2) = Range("G" & i).Value
            .List(.ListCount - 1, 3) = Range("K" & i).Value
        End If
    Next
    UserForm1.Show
    End With
End Sub
 
Last edited:
Upvote 0
You are probably referring to the ColumnCount property, which sets the number of columns in a listbox:
Code:
Sub Pending()
    Dim r As Long
    Dim msg As String, i As Long, a As Variant
    With UserForm1.ListBox1
    .Clear
    .ColumnCount = 4
    .ColumnWidths = "25;25;25;15"
    For i = 13 To 100
        If Range("F" & i) <> "" And Cells(i, 14).Value = "" Then
            .AddItem Range("E" & i).Value
            .List(.ListCount - 1, 1) = Range("F" & i).Value
            .List(.ListCount - 1, 2) = Range("G" & i).Value
            .List(.ListCount - 1, 3) = Range("K" & i).Value
        End If
    Next
    UserForm1.Show
    End With
End Sub
Sir, After run it, there is no data in ListBox, May you advise me solution accordingly?
 
Last edited:
Upvote 0
I changed the high value of the loop counter to 100 rather than the 100000 you had. FOrgot to change it back, apologies!
 
Upvote 0
Dear All Masters,

I want to make my ListBox to be tidy with ListIndex. It means that I want to have straight line column in ListBox. Currently, I don't know how to input ListIndex into below code. Pls kindly help me accordingly. Thanks all Masters.

Hi, virakbot
Glad that jkpieterse found a solution for you.
But just an idea:
Do you really have 100000 rows of data?
I guess it will take long to loop directly in the range as your code does. So you may want to use an array and do the loop there, it will be faster.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top