Using LastRow Code to Fill ComboBox.List But it Doesn't Work With Only 1 Row of Data

RunTime91

Active Member
Joined
Aug 30, 2012
Messages
290
Office Version
  1. 365
Greetings ~

The below code works great as long as there is more than one row of data to populate the List.

Code:
CmbCallRsn.List = Ws1.Range("A2:A" & Lr).Value

However to insure accurate results when there is only one row of data to populate I use the following and get a "Could Not Set The List Property. Invalid Property Array Index"
Code:
If Lr = 2 Then 
CmbCallRsn.List = Ws1.Range("A2").Value
Else
CmbCallRsn.List = Ws1.Range("A2:A" & Lr).Value
End If

Thank You for any assistance

RT91
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
That error message indicates you're trying to access an index or 0 or negative. How are you getting the Lr?
 
Upvote 0
Thanks Cubist for chiming in...

Here is the line of code I'm using for LastRow
Code:
Lr = Ws1.Cells(Rows.Count, 1).End(xlUp).Row
 
Upvote 0
Thanks. After you run the code, hover over Lr. What is the value of Lr?
 
Upvote 0
The value is 2 ...

I have tried the following:
Code:
CmbCallRsn.List = Ws1.Range("A:A" & Lr).Value
CmbCallRsn.List = Ws1.Range("A:" & Lr).Value
CmbCallRsn.List = Ws1.Range("A1:A" & Lr).Value - (This doesn't error but predictably it pulls the header row into the ComboBox DropDown)
CmbCallRsn.List = Ws1.Range("A" & Lr).Value
et.al
 
Upvote 0
Try this.

VBA Code:
If Lr = 2 Then 
CmbCallRsn.AddItem = Ws1.Range("A2").Value
Else
CmbCallRsn.List = Ws1.Range("A2:A" & Lr).Value
End If
 
Upvote 0
hm...Can you post your entire code?
 
Upvote 0
Absolutely...Here you go...

Code:
Dim Ws1 As Worksheet
Set Ws1 As ThisWorkbook.Sheets("UndrPaidTxs")
Lr = Ws1.Cells(Rows.Count, 1).End(xlUp).Row
If Lr = 2 Then 
CmbCallRsn.List = Ws1.Range("A2").Value
Else
CmbCallRsn.List = Ws1.Range("A2:A" & Lr).Value
End If
End Sub
 
Upvote 0
Try this. I think you need to use the Transpose function to convert the column of values to a row, since ComboBoxes expect a single-dimensional array.

VBA Code:
Sub PopulateComboBox()
    Dim Ws1 As Worksheet
    Set Ws1 = ThisWorkbook.Sheets("UndrPaidTxs")
    
    Dim Lr As Long
    Lr = Ws1.Cells(Ws1.Rows.Count, 1).End(xlUp).Row
    
    If Lr = 2 Then
        CmbCallRsn.AddItem Ws1.Range("A2").Value
    Else
        CmbCallRsn.List = Application.WorksheetFunction.Transpose(Ws1.Range("A2:A" & Lr).Value)
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,195
Members
452,616
Latest member
intern444

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