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
279
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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Is this ActiveX control for form control combo box?
 
Upvote 0
This works for ActiveX for me and does not need the if statement for Lr =2. Try.
VBA Code:
Sub PopulateComboBox()
    Dim Ws1 As Worksheet
    Set Ws1 = ThisWorkbook.Sheets("UndrPaidTxs")
    Dim comboBox As Object
    Dim Lr As Long
    Lr = Ws1.Cells(Ws1.Rows.Count, 1).End(xlUp).Row
 
    Set comboBox = Ws1.OLEObjects("CmbCallRsn").Object
    comboBox.Clear

    For Each c In Ws1.Range("A2:A" & Lr)
        comboBox.AddItem c.Value
    Next c
End Sub
 
Last edited:
Upvote 0
@RunTime91
Try:
VBA Code:
If Lr = 2 Then 
CmbCallRsn.List = Array(Ws1.Range("A2").Value)
Else
CmbCallRsn.List = Ws1.Range("A2:A" & Lr).Value
End If
 
Upvote 0
Works Like a Dream, Akuini!!

Thank You Both Akuini & Cubist for helping me on this one

It was much more difficult than I had anticipated it would be...
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,216,045
Messages
6,128,484
Members
449,455
Latest member
jesski

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