Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Populate items in combobox based on header

0

Hi,

I would populate items in combobox2 based on selected item from combobox1 , the header in combobox1 matchs based on header for row1 inside sheet.

example :if the Selected item from combobox1 is Cust then will match with header  inside sheet and populate items  in combobox2  based on selected header in combobox1 . so should show :

MLLK

MLKLL

KMML

but if it's possible I don't need duplicates items .

I have code but I need correcting.

I will add new columns so header will increase in combobox1

Private Sub ComboBox1_Change()
Application.ScreenUpdating = False
    Dim LastRow As Long, header As Range, foundHeader As Range, lCol As Long, srcWS As Worksheet
    Set srcWS = Sheets("ppr")
   
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lCol = srcWS.Cells(1, Columns.Count).End(xlToLeft).Column
    For Each header In srcWS.Range(srcWS.Cells(1, 1), srcWS.Cells(1, lCol))
        Set foundHeader = srcWS.Rows(1).Find(header, LookIn:=xlValues, lookat:=xlWhole)
        If ComboBox1 = foundHeader Then
            ComboBox2.Value = srcWS.Cells(LastRow, foundHeader.Column)
        End If
    Next header
    Application.ScreenUpdating = True
End Sub

thanks

Answer
Discuss

Discussion

Hi again Malkal. I'm mobile at present (with just my phone) so can't see your code. Could you add your code to the question please? (I might get chance to reply late tonight when I get home if I can see the code ahead) . 
John_Ru (rep: 6537) Nov 20, '24 at 9:59 am
Hi John,
I added the code.
Malkal (rep: 24) Nov 20, '24 at 12:03 pm
Thanks Malkal- that helped. Please see my Answer.
John_Ru (rep: 6537) Nov 20, '24 at 6:34 pm
Add to Discussion

Answers

0
Selected Answer

Malkal

In the attached revised file, I've modified your code to use a VBA Dictionary to eliminate duplicates in values (and to add only unique values to Combobox2 of your UserForm). See comments and changes in bold:

Private Sub ComboBox1_Change()

    Dim LastRow As Long, header As Range, lCol As Long, srcWS As Worksheet
    Dim dict As Object, n As Integer

    Set srcWS = Sheets("ppr")
    ' create a new empty dicionary
    Set dict = CreateObject("scripting.dictionary")

    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    ' find last row of data table
    With srcWS.Cells(1, 1).CurrentRegion
        lCol = .Columns(.Columns.Count).Column
    End With
    ' clear CB2
    ComboBox2.Clear
    ' loop along row 1 in current region
    For Each header In srcWS.Range(srcWS.Cells(1, 1), srcWS.Cells(1, lCol))
        ' see if CB1 matches row header
        If ComboBox1 = header.Value Then
            ' if so, loop down column...
            For n = 2 To LastRow
                ' ... and use ditionary to find only unique values
                If Not dict.exists(Cells(n, header.Column).Value) Then
                    ' add these to dictionary and CB2
                    dict.Add Key:=Cells(n, header.Column).Value, Item:=""
                    ComboBox2.AddItem Cells(n, header.Column).Value
                End If
            Next n
        End If
    Next header

End Sub

Note that

  1. you didn't need your variable foundheaders nor to disable screen updating (for a UserForm)
  2. I used a different technique (involving CorrentRegion) to find the last column of the data table (and ignore your list of headings, currently in column H).

Hope this works well for you.

Discuss

Discussion


(and ignore your list of headings, currently in column H)
I'm not really sure waht you mean .I thought the code will load them automatically !
the code works perfectly.
thanks for your solution .
Malkal (rep: 24) Nov 21, '24 at 1:46 am
Thanks for selecting my Answer Malkal.

On "(and ignore your list of headings, currently in column H)", I meant that your method of finding lcolumn would make it 8 (since H is the last used  column in row 1) whereas mine makes it 6 (F) and doesn't test G or H.
John_Ru (rep: 6537) Nov 21, '24 at 3:42 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login