Populate combobox from table if offset cell contains certain data

Diving_Dan

Board Regular
Joined
Oct 20, 2019
Messages
161
Hi all,

I've got the following code that is working fine for me but I need to adapt it and not quite sure how. At present it puts all data from column 8 of a table into a combobox.

What I would like it to do is only pull data from column 8 of the table if column 2 of that row contains "A", "B" or "C".

VBA Code:
Private Sub cmdViewAmendStaff_Click()
        
    With frmSelectStaff
    .OptionButton1.Value = True
    .cboStaff.List = Sheets("A Team").ListObjects("Table1").ListColumns(8).DataBodyRange.Value
    .Show
    End With
    
End Sub

Any help is appreciated as always.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Are the values in col 8 unique?
 
Upvote 0
The values in column 8 are all unique. They contain staff names and. Column 2 contains their pay grade, I only want to populate the combobox with staff who has a paygrade of A, B or C.
 
Upvote 0
OK, how about
VBA Code:
Private Sub cmdViewAmendStaff_Click()
   Dim Cl As Range
      
   frmSelectStaff.OptionButton1.Value = True
   With CreateObject("scripting.dictionary")
      For Each Cl In Sheets("A Team").ListObjects("Table1").ListColumns(8).DataBodyRange
         Select Case Cl.Offset(, -6).Value
            Case "A", "B", "C"
               .Item(Cl.Value) = Empty
         End Select
      Next Cl
      frmSelectStaff.cboStaff.List = .Keys
    End With
    frmSelectStaff.Show
End Sub
 
Upvote 0
Thanks Fluff, that works great. However I've just realised that the code actually needs to go into when the userform initializes. I was going to adapt that code for a slightly different job.

I've tried putting your code into the userform Initialize sub but the combobox remains empty, not sure where I'm going wrong.
 
Upvote 0
How about
VBA Code:
Private Sub UserForm_Initialize()
   Dim Cl As Range
      
   Me.OptionButton1.Value = True
   With CreateObject("scripting.dictionary")
      For Each Cl In Sheets("A Team").ListObjects("Table1").ListColumns(8).DataBodyRange
         Select Case Cl.Offset(, -6).Value
            Case "A", "B", "C"
               .Item(Cl.Value) = Empty
         End Select
      Next Cl
      Me.cboStaff.List = .Keys
    End With
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,737
Messages
6,126,558
Members
449,318
Latest member
Son Raphon

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