Display Listview data in Userform

van0912

New Member
Joined
Jul 27, 2022
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I would like to display data from an excel sheet ("Test") in an UserForm listview. I have 12 columns and 500 rows in my sheet table. In the code, message error keep pop-in as "Run-time error 13. Type mismatch". Could you please help me with that issue?

Thank you 🙏

VBA Code:
Private Sub UserForm_Initialize()

Dim ws As Worksheet
Dim rg As Range
Dim i As Integer
Dim j As Integer

Set ws = ThisWorkbook.Sheets("Test")
Set rg = ws.Range("refTab")

With UserForm3.ListView1

For i = 1 To 12
.ColumnHeaders.Add , , rg.Offset(0, i - 1)
Next i

For i = 1 To 500

.ListItems.Add , , rg.Offset(i, 0)
Next i

For i = 1 To 500
For j = 1 To 11

.ListItems(i).ListSubItems.Add , , rg.Offset(i, j)
Next j
Next i

.View = lvwReport

End With

End Sub
 
Last edited by a moderator:

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
The reason you're getting that error is that rg.Offset(0, i - 1), rg.Offset(i, 0), and rg.Offset(i, j) each return a range, not a single value. Hence the type mismatch error. Try the following instead...

VBA Code:
Private Sub UserForm_Initialize()

    Dim ws As Worksheet
    Dim rg As Range
    Dim li As ListItem
    Dim i As Integer
    Dim j As Integer
    
    Set ws = ThisWorkbook.Sheets("Test")
    Set rg = ws.Range("refTab")
    
    With rg
        For i = 1 To .Columns.Count
            Me.ListView1.ColumnHeaders.Add , , rg.Cells(1, i).Text
        Next i
    End With
    
    With rg
        For i = 2 To .Rows.Count
            Set li = Me.ListView1.ListItems.Add(, , rg.Cells(i, 1).Text)
            For j = 2 To .Columns.Count
                li.ListSubItems.Add , , rg.Cells(i, j).Text
            Next j
        Next i
    End With
    
    Me.ListView1.View = lvwReport

End Sub

Hope this helps!
 
Upvote 0
Solution
The reason you're getting that error is that rg.Offset(0, i - 1), rg.Offset(i, 0), and rg.Offset(i, j) each return a range, not a single value. Hence the type mismatch error. Try the following instead...

VBA Code:
Private Sub UserForm_Initialize()

    Dim ws As Worksheet
    Dim rg As Range
    Dim li As ListItem
    Dim i As Integer
    Dim j As Integer
   
    Set ws = ThisWorkbook.Sheets("Test")
    Set rg = ws.Range("refTab")
   
    With rg
        For i = 1 To .Columns.Count
            Me.ListView1.ColumnHeaders.Add , , rg.Cells(1, i).Text
        Next i
    End With
   
    With rg
        For i = 2 To .Rows.Count
            Set li = Me.ListView1.ListItems.Add(, , rg.Cells(i, 1).Text)
            For j = 2 To .Columns.Count
                li.ListSubItems.Add , , rg.Cells(i, j).Text
            Next j
        Next i
    End With
   
    Me.ListView1.View = lvwReport

End Sub

Hope this helps!
Hello Domenic,
Thank you for your help on this! I managed to make it works with the following code and added a combobox that displays value of the second column (range I). Any chance you know how I can do to make it filter on the listview? Fo example if I select a value in the combobox?
Thanks again!

VBA Code:
Private Sub UserForm_Initialize()

Dim ws As Worksheet
Dim rg As Range
Dim i As Long
Dim j As Long

Set ws = ThisWorkbook.Sheets("Test")  
Set rg = ws.Range("ReferenceTab")            

With UserForm3.ListView1             

For i = 1 To 12         
        .ColumnHeaders.Add , , rg.Offset(0, i - 1)
Next i

For i = 1 To 500
        .ListItems.Add , , rg.Offset(i, 0)
Next i

For i = 1 To 500   
For j = 1 To 11    
            .ListItems(i).ListSubItems.Add , , rg.Offset(i, j)
Next j
Next i

    .View = lvwReport

End With

Dim v, e
With Sheets("Test").Range("I4:I500")
v = .Value
End With

With CreateObject("scripting.dictionary")
.comparemode = 1

For Each e In v
If Not .exists(e) Then .Add e, Nothing
Next
If .Count Then Me.ComboBox1.List = Application.Transpose(.Keys)

End With
       
End Sub
 
Upvote 0
You're very welcome.

For your new question, though, please start a new a thread.

Cheers!
 
Upvote 0
I managed to make it works with the following code

The code claimed to be working:

VBA Code:
.ColumnHeaders.Add , , rg.Offset(0, i - 1)
...
.ListItems.Add , , rg.Offset(i, 0)
...
.ListItems(i).ListSubItems.Add , , rg.Offset(i, j)

However, it shouldn't work as it was posted as the solution. Actually, I am sure about it but still tested the provided code to verify that.

As @Domenic also explained, rg.Offset(rows, cols) will return a range object, not a string value that each Add method requires in the provided implementation. Although what I am going to suggest is not the best way to do that and I would surely stick with the perfect dynamic list view implementation that @Domenic provided that also is not limited to a certain number of columns and rows in the provided range, in the best scenario, .Cells(1) should be added after each Offset property to make the current code work as shown in the sample line below.

VBA Code:
rg.Offset(0, i - 1).Cells(1)

@van0912 - We'd like to make sure that this question's marked solution actually works to help future readers. So, if you could post your working code then it is perfectly fine to mark your own post as the solution - assuming it is using a different method other than the provided answer. Or, if it works as you posted, then it means the ListView control that comes with the Excel 2016 version should be working differently than the Excel 365 version that any future reader (including myself) would also like to know about this difference as a reference.

Otherwise, I will switch the marked solution post to the second post in this thread to help future readers.
 
Upvote 0
Hello @smozgur thank you for your input! I've tried to add .cells(1) in Domenic's code but could'nt get the data in the listview (only RefTab header appears)... Should I add .cells(1) in my code?
 
Upvote 0
Hello @smozgur thank you for your input! I've tried to add .cells(1) in Domenic's code but could'nt get the data in the listview (only RefTab header appears)...
Domenic's code works without any change if the correct range is provided (in fact, it is already using the Cells property).

Should I add .cells(1) in my code?
Yes. What I suggested is supposed to be added to the last code you posted.
 
Upvote 0
Domenic's code works without any change if the correct range is provided (in fact, it is already using the Cells property).


Yes. What I suggested is supposed to be added to the last code you posted.
Ok I understand better, thank you! I've added .cells(1) :)
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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