Populating a combobox from another combobox

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
As of now I have two tabs labeled hats and shoes. The first combo box displays in this case Hats and Shoes.
Code:
Private Sub cmbSDPFLine_Enter()    Dim i As Long
    cmbSDPFLine.Clear
    cmbPrdCde.Enabled = False
    cmbPrdCde.Clear
    txtbxPrdctNm.Text = ""
    
    WS_Count = ActiveWorkbook.Worksheets.Count
    
    For i = 5 To WS_Count
        cmbSDPFLine.AddItem Sheets(i).Name
    Next
    
End Sub
If the user selects hats from the first combo box, on the spreadsheet with the tab labeled "Hats", starting in cell "A3" starts the list of different types of hats. As the user adds more hats to column A it should be added to the combo box. I thought the below code would work but it displays a "Run-time error '381': Could not set the List property. Invalid property array index."
Code:
Private Sub cmbPrdCde_Enter()    
    If cmbSDPFLine.Value = "Hats" Then
        cmbPrdCde.Clear
        cmbPrdCde.Value = ""
        With ThisWorkbook.Worksheets("Hats")
            Me.cmbPrdCde.List = .Range("A3").End(xlDown)


        End With
End SUB
Thank you to all for your help
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How about
Code:
   Me.cmbPrdCde.List = Application.Transpose(.Range("A3", .Range("A" & Rows.Count).End(xlUp)))
 
Upvote 0
Also rather than checking for the value of the combobox you can do
Code:
Private Sub cmbPrdCde_Enter()
        cmbPrdCde.Clear
        cmbPrdCde.Value = ""
        With ThisWorkbook.Worksheets(cmbSDPFLine.Value)
            Me.cmbPrdCde.List = Application.Transpose(.Range("A3", .Range("A" & Rows.Count).End(xlUp)))
        End With
End Sub
 
Upvote 0
Code:
.Range("A3", .Range("A" & Rows.Count).End(xlUp))
will give a 2d array, but the combobox needs a 1d list, The Transpose turns the 2d array into a 1d array
 
Upvote 0
How would I go about concatenating columns A and column B with this code. Is it possible? Column A will contain the item number for the hats and Column B will contain the name of the hat. Thank You.
 
Upvote 0
How about
Code:
Private Sub cmbPrdCde_Enter()
Dim ary As Variant, nary As Variant, r As Long
   cmbPrdCde.Clear
   cmbPrdCde.Value = ""
   With ThisWorkbook.Worksheets(cmbSDPFLine.Value)
      ary = .Range("A3", .Range("A" & Rows.Count).End(xlUp).Offset(, 1))
      ReDim nary(1 To UBound(ary))
         For r = 1 To UBound(ary)
            nary(r) = ary(r, 1) & ", " & ary(r, 2)
         Next r
   Me.cmbPrdCde.List = nary
   End With
End Sub
 
Upvote 0
That worked wonderfully. Thank you. One question, is it possible to display just the Hat description into a read only textbox. For example if the user selects item 324-6 Tophat-Black. How would I just display "Tophat-Black" in a textbox. Thank you again.
 
Upvote 0
You have already started a new thread for this.
Please do not post the same question multiple times, as per Rule#12
 
Upvote 0

Forum statistics

Threads
1,215,639
Messages
6,125,968
Members
449,276
Latest member
surendra75

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