2 ComboBox

haseft

Active Member
Joined
Jun 10, 2014
Messages
321
hi,
I have a userform with 2 listbox,
ListBox1, pupulate uniqe items from column B in Sheet1, workning well,

Need help with ComboBox2.
want to fill ComboBox2 with criteria choosen fron ComboBox1
ComboBox2 should show item from column A in Sheet1
Tanks,

VBA Code:
Private Sub UserForm_Initialize()
Dim myCollection As Collection, cell As Range

On Error Resume Next
Set myCollection = New Collection
With ComboBox1
  .Clear
For Each cell In Sheets("Sheet1").Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row)
If Len(cell) <> 0 Then
  Err.Clear
  myCollection.Add cell.Value, cell.Value
  If Err.Number = 0 Then .AddItem cell.Value
End If
Next cell
End With
ComboBox1.ListIndex = 0

End Sub

Private Sub ComboBox1_Change()
'want to fill ComboBox2 with criteria choosen from ComboBox1
'ComboBox2 should show items from column A in Sheet1

With ComboBox2
.ColumnCount = 1
.ColumnWidths = "50"
.ColumnHeads = True
[B]'how ?[/B]


End With
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
How about
Rich (BB code):
Dim UfDic As Object

Private Sub ComboBox1_Change()
   Me.ComboBox2.Clear
   Me.ComboBox2.List = UfDic(Me.ComboBox1.Value).keys
End Sub

Private Sub UserForm_Initialize()
   Dim Cl As Range
   Set UfDic = CreateObject("scripting.dictionary")
   UfDic.CompareMode = 1
   With Sheets("Sheet1")
      For Each Cl In .Range("B2", .Range("B" & Rows.Count).End(xlUp))
         If Not UfDic.Exists(Cl.Value) Then UfDic.Add Cl.Value, CreateObject("scripting.dictionary")
         UfDic(Cl.Value)(Cl.Offset(, -1).Value) = Empty
      Next Cl
   End With
   Me.ComboBox1.List = UfDic.keys
End Sub
The line in blue must go at the very top of the module, before any code
 
Upvote 0
Thanks Fluff,
its workning
I have another problem,

'under ComboBox2 there is 3 texboxes
'i want to fill these texboxes data from column E, K and M in Sheet1
'based with criteria choosen from ComboBox2

VBA Code:
Private Sub ComboBox2_Change()

'TextBox1.value = data from clomn E
'TextBox2.value = data from clomn K
'TextBox3.value = data from clomn M

'how

End Sub
 
Upvote 0
How about
VBA Code:
Dim UfDic As Object

Private Sub ComboBox1_Click()
   Me.ComboBox2.Clear
   Me.ComboBox2.List = UfDic(Me.ComboBox1.Value).Keys
End Sub

Private Sub ComboBox2_Click()
   With UfDic(Me.ComboBox1.Value)
      Me.TextBox1 = .Item(Me.ComboBox2.Value)(0)
      Me.TextBox2 = .Item(Me.ComboBox2.Value)(1)
      Me.TextBox3 = .Item(Me.ComboBox2.Value)(2)
   End With
End Sub

Private Sub UserForm_Initialize()
   Dim Cl As Range
   Set UfDic = CreateObject("scripting.dictionary")
   UfDic.CompareMode = 1
   With Sheets("Sheet1")
      For Each Cl In .Range("B2", .Range("B" & Rows.Count).End(xlUp))
         If Not UfDic.Exists(Cl.Value) Then UfDic.Add Cl.Value, CreateObject("scripting.dictionary")
         UfDic(Cl.Value)(Cl.Offset(, -1).Value) = Array(Cl.Offset(, 3).Value, Cl.Offset(, 9).Value, Cl.Offset(, 11).Value)
      Next Cl
   End With
   Me.ComboBox1.List = UfDic.Keys
End Sub
 
Upvote 0
not workning,
get run error 13, incompatible
on row in Sub ComboBox2_Click()
Me.TextBox1 = .Item(Me.ComboBox2.Value)(0) ???
 
Upvote 0
Did you change the code in the Initalize event?
 
Upvote 0
Do you have any error in col E like #N/A, #NAME, #VALUE etc?
 
Upvote 0
The notes shouldn't make any difference, what is the exact error message that you get?
 
Upvote 0

Forum statistics

Threads
1,215,203
Messages
6,123,627
Members
449,109
Latest member
Sebas8956

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