Type Mismatch, what am I doing wrong?

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
I am trying to populate a ListBox with a dynamic range, but I get a mismatch error on the .rowsource assignment. Hoping someone can shed some light on how I am supposed to assign the dynamic range to the row source.

VBA Code:
Private Sub AddButton_Click()
'Add Item to Order
    Dim OrdSum As Worksheet
    Dim Addme As Range
    
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim StartCell As Range
    
    Set StartCell = Range("A1")
    
    ThisWorkbook.Sheets("Order Summary").UsedRange
    LastRow = StartCell.SpecialCells(xlCellTypeLastCell).Row
    LastColumn = StartCell.SpecialCells(xlCellTypeLastCell).Column
          
    Set OrdSum = Sheet4
    Set Addme = OrdSum.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)   
    
    With OrdSum
        
        Addme.Value = ProductBox.Value
        Addme.Offset(0, 1).Value = PID.Value
        Addme.Offset(0, 2).Value = CaseQty.Value
        Addme.Offset(0, 3).Value = PackSizeBox.Value
        Addme.Offset(0, 4).Value = StageBox.Value
        Addme.Offset(0, 5).Value = AssBox.Value
        Addme.Offset(0, 6).Value = ColourBox.Value
        Addme.Offset(0, 7).Value = IIf(OrderEntry.CoverSelect.Value = True, CoverBox.Value, "None")
        Addme.Offset(0, 8).Value = IIf(OrderEntry.OrnamentSelect.Value = True, OrnamentBox.Value, "")
        Addme.Offset(0, 9).Value = IIf(OrderEntry.UPCSelect.Value = True, UPCBox.Value, "")
        Addme.Offset(0, 10).Value = IIf(OrderEntry.CareTagSelect.Value = True, "Yes", "")
        Addme.Offset(0, 11).Value = IIf(OrderEntry.InsulationSelect.Value = True, "Yes", "")
        Addme.Offset(0, 12).Value = IIf(OrderEntry.SleeveSelect.Value = True, "Yes", "")
        Addme.Offset(0, 13).Value = NotesBox.Value
        
    End With
    
    Clear
        
    With ListBox1
        .ColumnCount = 14
        .ColumnHeads = True
        .ColumnWidths = "90,35,40,50,50,60,95,60,65,90,50,60,60,300"
        .RowSource = OrdSum.Range(StartCell, OrdSum.Cells(LastRow, LastColumn))
        .TopIndex = .ListCount - 1
    End With
    
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Rowsource needs to be the address of the range.
 
Upvote 0
VBA Code:
.RowSource = "'" & OrdSum.Name & "'!" & OrdSum.Range(StartCell, OrdSum.Cells(LastRow, LastColumn)).Address
 
Upvote 0
Solution
VBA Code:
.RowSource = "'" & OrdSum.Name & "'!" & OrdSum.Range(StartCell, OrdSum.Cells(LastRow, LastColumn)).Address
I have tried changing the way I do my dynamic range to account for if the values are empty with the following rowsource, but I now get Invalid property value. Could you point out what I am doing wrong?

The error only highlights the Rowsource argument after the Else line in the if statement.

VBA Code:
Dim iRow As Long
iRow = [Counta(Order Summary!A:A)]

With ListBox1
        .ColumnCount = 14
        .ColumnHeads = True
        .ColumnWidths = "90,35,40,50,50,60,95,60,65,90,50,60,60,300"
       
        If iRow > 1 Then
            .RowSource = "Order Summary!A2:N" & iRow
        Else
            .RowSource = "Order Summary!A2:N2"
        End If
        '.RowSource = "'" & OrdSum.Name & "'!" & OrdSum.Range(StartCell, OrdSum.Cells(LastRow, LastColumn)).Address
               
        .TopIndex = .ListCount - 1
   
    End With
 
Upvote 0
The sheet name has spaces in it, so has to be enclosed in single quotes, just as it would in a formula - i.e. 'Order Summary'!A2:N2 not Order Summary!A2:N2
 
Upvote 0
The sheet name has spaces in it, so has to be enclosed in single quotes, just as it would in a formula - i.e. 'Order Summary'!A2:N2 not Order Summary!A2:N2
Always getting owned by the syntax. Thanks a lot, appreciate your assistance.
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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