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

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
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,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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