Add Items to Combo Box

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
I want to add items to a combo box however I want to restrict the range to A3:A12. Also, I notice in the current code, when I click the drop down arrow, it displays the empty ranges. Is there a way to not show these ranges if the cells are empty? Can someone assist with this task. Thank you kindly.


Code:
Set rng = Range("A3", Range("A3").End(xlDown))For Each cell In rng.Cells
    cbName.AddItem cell.Value
Next cell
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this:

This script only looks at range A3 To A12 like you asked for.
Code:
Private Sub CommandButton1_Click()
'Modified  11/7/2018  4:59:47 AM  EST
Dim r As Range
ComboBox1.Clear
    For Each r In Range("A3:A12")
        If r.Value <> "" Then ComboBox1.AddItem r.Value
    Next
End Sub
 
Last edited:
Upvote 0
Now if you wanted the Range to be From A3 to the last value in column A you would use this:

Code:
Private Sub CommandButton1_Click()
'Modified  11/7/2018  5:04:08 AM  EST
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim r As Range
ComboBox1.Clear
    For Each r In Range("A3:A" & Lastrow)
        If r.Value <> "" Then ComboBox1.AddItem r.Value
    Next
End Sub
 
Last edited:
Upvote 0
What am I doing wrong?

Code:
Dim r As Variant

On Error Resume Next
r = Application.WorksheetFunction.Match(cbName, Range("A3:A12"), 0)


MsgBox Range("e" & r)
Exit Sub
 
Upvote 0
Remove the Worksheetfunction to leave
Code:
r = Application.Match(cbName, Range("A3:A12"), 0)
 
Upvote 0

Forum statistics

Threads
1,215,203
Messages
6,123,627
Members
449,109
Latest member
Sebas8956

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