How to Load ListView the Fastest

JarekM

Board Regular
Joined
Nov 13, 2018
Messages
86
Hi, can anyone help me figure out (if there is a way) how to write this code in another way? There is a lot of data and it takes a while for it to load, so is there a way to make this code run faster, or does ListView just take so long to load?

VBA Code:
 'load list view
lastLine = Sheet1.Cells(Sheet1.Cells.Rows.Count, "a").End(xlUp).row
    ListView1.ListItems.Clear
    For X = 2 To lastLine
        Set li = ListView1.ListItems.Add(Text:=Sheet1.Cells(X, "a").Value)
        li.ListSubItems.Add Text:=Sheet1.Cells(X, "c").Value
        li.ListSubItems.Add Text:=Sheet1.Cells(X, "d").Value
        li.ListSubItems.Add Text:=Sheet1.Cells(X, "q").Value
        li.ListSubItems.Add Text:=Sheet1.Cells(X, "r").Value
        li.ListSubItems.Add Text:=Sheet1.Cells(X, "s").Value
        li.ListSubItems.Add Text:=Sheet1.Cells(X, "u").Value
        li.ListSubItems.Add Text:=Sheet1.Cells(X, "l").Value
        li.ListSubItems.Add Text:=Sheet1.Cells(X, "m").Value
    Next

Thank you for any help.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Reading values from the worksheet is inefficient. Instead, first transfer the worksheet values to an array, and then loop through the array to fill your listview...

VBA Code:
    With Sheet1
   
        Dim lastRow As Long
        lastRow = .Cells(.Rows.Count, "a").End(xlUp).Row
       
        Dim data As Variant
        data = .Range("A1:U" & lastRow).Value
       
    End With
   
    Dim colIndices As Variant
    colIndices = Array(3, 4, 17, 18, 19, 21, 12, 13)

    Dim li As ListItem
    Dim rowIndex As Long
    Dim columnIndex As Long
    With ListView1
        .ListItems.Clear
        For rowIndex = 2 To lastRow
            Set li = .ListItems.Add(Text:=data(rowIndex, 1))
            For columnIndex = LBound(colIndices) To UBound(colIndices)
                li.ListSubItems.Add Text:=data(rowIndex, colIndices(columnIndex))
            Next columnIndex
        Next rowIndex
    End With

Hope this helps!
 
Upvote 0
Solution
Reading values from the worksheet is inefficient. Instead, first transfer the worksheet values to an array, and then loop through the array to fill your listview...

VBA Code:
    With Sheet1
  
        Dim lastRow As Long
        lastRow = .Cells(.Rows.Count, "a").End(xlUp).Row
      
        Dim data As Variant
        data = .Range("A1:U" & lastRow).Value
      
    End With
  
    Dim colIndices As Variant
    colIndices = Array(3, 4, 17, 18, 19, 21, 12, 13)

    Dim li As ListItem
    Dim rowIndex As Long
    Dim columnIndex As Long
    With ListView1
        .ListItems.Clear
        For rowIndex = 2 To lastRow
            Set li = .ListItems.Add(Text:=data(rowIndex, 1))
            For columnIndex = LBound(colIndices) To UBound(colIndices)
                li.ListSubItems.Add Text:=data(rowIndex, colIndices(columnIndex))
            Next columnIndex
        Next rowIndex
    End With

Hope this helps!
Thank you for the reply. You resolved the issue and the program is working 100% faster. Although I still have one more question, how can I update this code if I want to add search to the ListView?
 
Upvote 0
Thank you for the reply. You resolved the issue and the program is working 100% faster.

That's great, glad I could help, and thanks for the feedback.

Although I still have one more question, how can I update this code if I want to add search to the ListView?

The code will depend on how you want the search to take place. For example...

1) Do you want to search for a listitem's text, subitems, or tag property?

2) Do you want to search for an exact match or partial match? If the latter, should the match occur at the beginning of the text string? Or can it occur anywhere within the text string?

However, since this is a new question, please start a new thread and post your question there.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,215,357
Messages
6,124,482
Members
449,165
Latest member
ChipDude83

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