ListBox List Fails When Table Has One Row

wsnyder

Board Regular
Joined
Sep 23, 2018
Messages
224
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Using Excel 365.

My code returns an error when the source Table has 1 Row.
How can I modify to handle whether the Table has 1 to n Rows?
Works great as-is as long as the Table has 2 or more rows.

Thanks
-w

VBA Code:
Private Sub UserForm_Initialize()

    'Objects
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim lo As ListObject
        
    'Initialize
        Set wb = ThisWorkbook
        Set ws = wb.Worksheets("dataSuppliers")
        Set lo = ws.ListObjects(1)
        
    'Populate listbox
        If lo.DataBodyRange.Rows.Count = 1 Then
            'Do something
        Else
            lstBoxSuppliers.List = lo.DataBodyRange.Value
        End If
        
    'Tidy up
        Set lo = Nothing
        Set ws = Nothing
        Set wb = Nothing
        
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I assume the table has only 1 column.
Try:
VBA Code:
    'Populate listbox
    Dim va
        If lo.DataBodyRange.Rows.Count = 1 Then
            ReDim va(1 To 1, 1 To 1)
            va(1, 1) = lo.DataBodyRange.Value
            lstBoxSuppliers.List = va
        Else
            lstBoxSuppliers.List = lo.DataBodyRange.Value
        End If
 
Upvote 0
Thanks Akuini,

Works great if there is only 1 Row in the Table.
However, as soon as I add a 2nd Row, it throws an error:
Run-time error '13':
Type Mismatch
Why is that?
Revised code snippet
VBA Code:
    'Populate listbox
        If lo.DataBodyRange.Rows.Count = 1 Then
            ReDim arr(1 To 1, 1 To 1)
            arr(1, 1) = lo.DataBodyRange.Value
            lstBoxSuppliers.List = arr
        Else
            lstBoxSuppliers.List = lo.DataBodyRange.Value
        End If
 
Upvote 0
This will work

VBA Code:
 On Error Resume Next
  If lo.DataBodyRange.Rows.Count = 1 Then
     'Do something
  Else
        lstBoxSuppliers.List = lo.DataBodyRange.Value
  End If
  On Error GoTo 0
 
Upvote 0
On which line it throws an error?
Debugger points here. So I assume it is the initialization event of the form
VBA Code:
Sub LoadSuppliersForm()
    frmSuppliers.Show vbModeless
End Sub

Here is the full code for the Initialization Event
Code:
Private Sub UserForm_Initialize()

    'Objects
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim lo As ListObject
        
    'Variables
        Dim arr As Variant
        
    'Initialize
        Set wb = ThisWorkbook
        Set ws = wb.Worksheets("dataSuppliers")
        Set lo = ws.ListObjects(1)
        
    'Populate listbox
        If lo.DataBodyRange.Rows.Count = 1 Then
            ReDim arr(1 To 1, 1 To 1)
            arr(1, 1) = lo.DataBodyRange.Value
            lstBoxSuppliers.List = arr
        Else
            lstBoxSuppliers.List = lo.DataBodyRange.Value
        End If
        
    'Tidy up
        Erase arr
        Set lo = Nothing
        Set ws = Nothing
        Set wb = Nothing
        
End Sub
 
Upvote 0
So I assume it is the initialization event of the form
In the Vb Editor click on Tools, Options, General & select "Break in Class Module", Ok.
That will enable you to see where the error is.
 
Upvote 0
Solution
Thanks Fluff,

The problem was Erase arr in Cleanup
As soon as I commented it out, the form loaded correctly with 2 items in the Table.

Thanks,
-w
 
Upvote 0
Glad you sorted it & thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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