Hi all
First of all I would say I'm new to excel vba and know very little about it. Most of the macros I've ever used were copied and adapted by me.
I've got a following problem.
I have a table with some data, where "A","B" and "C" columns are more and more detailed description of a place. For example, "A1" = Europe, "B1" = England, "C1" = London.
"D" column is a value (expenses in this particular place for a period of time). There are a significant amount of rows with different places (up to 200 yet)
I've got 3 combo boxes with an available list of choices in the second and third depending on the choices in the previous ones. For example, if choose "Europe" in the first one, I can choose "France" or "England" etc. and then if I choose "England" I can go for "London", "Manchester" etc. Moreover, this data may repeat a few times. For example, there might be 5 "Europe" "England" "London" across the rows. The only difference will be in the value in "D" Column. I can continue these cascading combo boxes so I'm able to see all the values for a particular place across the rows. I've found a following code for that and adapted it a bit
Private Sub UserForm_Initialize()
'Load UserForm1
Place Range([A2], [A2].End(xlDown))
End Sub
Sub Place(Data As Range)
Dim d, cel As Range
Set d = CreateObject("Scripting.Dictionary")
For Each cel In Data
On Error Resume Next
d.Add cel.Text, cel.Text 'Add some keys and items
Next
ComboBox1.List() = d.items
End Sub
Private Sub ComboBox1_Change()
ComboBox2.Clear
'ComboBox2.TopIndex = ComboBox2.ListIndex
Country Range([B2], [B2].End(xlDown))
End Sub
Sub Country(Data As Range)
Dim d, cel As Range
Set d = CreateObject("Scripting.Dictionary")
For Each cel In Data
If cel.Offset(, -1) = ComboBox1.Text Then
On Error Resume Next
d.Add cel.Text, cel.Text
End If
Next
ComboBox2.List() = d.items
End Sub
Private Sub ComboBox2_Change()
ComboBox3.Clear
City Range([C2], [C2].End(xlDown))
End Sub
Sub City(Data As Range)
Dim d, cel As Range
Set d = CreateObject("Scripting.Dictionary")
For Each cel In Data
If cel.Offset(, -1) = ComboBox2.Text And cel.Offset(, -2).Text = ComboBox1.Text Then
On Error Resume Next
d.Add cel.Text, cel.Text
End If
Next
ComboBox3.List() = d.items
End Sub
Private Sub ComboBox3_Change()
ComboBox4.Clear
Value Range([D2], [D2].End(xlDown))
End Sub
Sub Value(Data As Range)
Dim d, cel As Range
Set d = CreateObject("Scripting.Dictionary")
For Each cel In Data
If cel.Offset(, -1) = ComboBox3.Text And cel.Offset(, -2) = ComboBox2.Text _
And cel.Offset(, -3) = ComboBox1.Text Then
On Error Resume Next
d.Add cel.Text, cel.Text
End If
Next
ComboBox4.List() = d.items
End Sub
However, I wonder if it's possible to use a Text Box instead of the fourth Combo Box, so I could see all the values and not choose them. More over, I wonder If it's possible to show the values in the Text Box for the places, if 3rd and even 2nd combo boxes are not chosen (To see all the values for the "Europe" or "England", without choosing a particular city).
Thank you so much much for your help in advance
Hope it's not too onerous.
Bookmarks