Populating ComboBox with criteria

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
477
Office Version
  1. 365
Platform
  1. Windows
My search did not come up with a UserForm example, so here I am.

I need to fill a ComboBox with data from a Sheet (Subdivisions) in a Range (SubdivisionsSubNames), but ONLY if there is a Yes in Column 17.

Here's what I have. I'm getting a run-time 424 error. I suspect that With Me.SubName.List is wrong.

VBA Code:
Private Sub UserForm_Initialize()

    Set wsSubdivisions = ThisWorkbook.Worksheets("Subdivisions")
    
    Dim c As Range
    With Me.SubName.List
    .Clear
    For Each c In Subdivisions.Range("SubdivisionsSubNames", Subdivisions.Range("SubdivisionsSubNames" & Rows.Count).End(3))
    If c.Offset(0, 17).Value = "Yes" Then
    .AddItem c.Value
    End If
    Next
    End With

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this:

VBA Code:
Private Sub UserForm_Initialize()
  Dim sh As Worksheet
  Dim c As Range
  
  Set sh = ThisWorkbook.Worksheets("Subdivisions")
  
  With Me.SubName
    .Clear
    For Each c In sh.Range("SubdivisionsSubNames")
      If c.Offset(0, 17).Value = "Yes" Then
        .AddItem c.Value
      End If
    Next
  End With
End Sub
 
Upvote 0
Try this:

VBA Code:
Private Sub UserForm_Initialize()
  Dim sh As Worksheet
  Dim c As Range
 
  Set sh = ThisWorkbook.Worksheets("Subdivisions")
 
  With Me.SubName
    .Clear
    For Each c In sh.Range("SubdivisionsSubNames")
      If c.Offset(0, 17).Value = "Yes" Then
        .AddItem c.Value
      End If
    Next
  End With
End Sub

No error, but the ComboBox is empty.
 
Upvote 0
In which column does the Range (SubdivisionsSubNames) start, in which column are the cells with the word "Yes"?
 
Upvote 0
Assuming Range("SubdivisionsSubNames") refer to col A, the offset should be 16 instead of 17.
 
Upvote 0
In which column does the Range (SubdivisionsSubNames) start, in which column are the cells with the word "Yes"?

It is always important to give an example, use the XL2BB tool to paste a minishet of your data:
Dante Amor
AQ
1AQ
2117
3a3q3
4a4Yes
5a5q5
6a6Yes
7a7q7
8a8Yes
9a9q9
10a10q10
Hoja5


If the above is correct, then the code:

VBA Code:
Private Sub UserForm_Initialize()
  Dim sh As Worksheet
  Dim c As Range
  
  Set sh = ThisWorkbook.Worksheets("Subdivisions")
  
  With Me.SubName
    .Clear
    For Each c In sh.Range("SubdivisionsSubNames")
      If c.Offset(0, 16).Value = "Yes" Then
        .AddItem c.Value
      End If
    Next
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,716
Members
449,093
Latest member
Mnur

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