VBA combobox modification with if statement

Pojzon

New Member
Joined
May 19, 2017
Messages
19
Hey there,
I've got something like this:
594160d1539602307-if-statement-in-combobox-sheet1.jpg
594161d1539602315-if-statement-in-combobox-sheet2.jpg


I need modification to this code:
Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Private Sub Worksheet_Activate()    
Dim a, e
    a = Sheets("Sheet1").Cells(1).CurrentRegion.Columns(1).Offset(1).Value
    With CreateObject("Scripting.Dictionary")
        .CompareMode = 1
        For Each e In a
            If e <> "" Then .Item(e) = Empty
        Next
        Me.ComboBox1.List = .keys
    End With 
End Sub
</code>

It fills my combobox1 with unique employee names, but I would like it to also take into account company name definied by combobox2 and linked into F2 cell. So something like if F2 = "XYZ" then fill combobox1 with unique employee names in that company else fill names with "ZYX" company.
Any help is greatly appreciated. Thank you!

Crossposted at:
https://www.excelforum.com/excel-programming-vba-macros/1249395-if-statement-in-combobox.html
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Its impossible to answer your question with what you have given. There are employee names in column A and a company name in F2. How are the employees matched to the company?
 
Upvote 0
They are matched in sheet1 (screen) employees are in column A and company in column B. One employee is always in only one company.
 
Upvote 0
Heres how you could get the cell values into the dictionary if they pass the test:

Code:
Dim lr As Long, d As Object, c As Range, k, s As String

lr = Range("A" & Rows.Count).End(xlUp).Row
Set d = CreateObject("scripting.dictionary")

For Each c In Range("A2:A" & lr)
    If c.Offset(0, 1).Value = Range("F2").Value Then
        s = Trim(c.Value)
        If Len(s) > 0 Then d(s) = d(s) + 1
    End If
Next

For Each k In d.keys
    Debug.Print k
Next
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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