3 Dependent Combo Box

Saru

New Member
Joined
May 15, 2020
Messages
19
Office Version
  1. 365
Platform
  1. Windows
I have three dependent combo box
I am populating first 'cboMainCat' on form activation. The two remaining (cboCat & cboSubCat) i am trying to populate on change event.
second one works fine but the third i am having trouble seeing what i am doing i can seem to populate the values.

First:
Private Sub UserForm_Activate()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("DropDowns")
Dim i As Integer

Me.cboMainCat.Clear
For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row ' becoz 1 is the header
If sh.Range("A" & i).Value = "Main_Cat" Then
Me.cboMainCat.AddItem sh.Range("B" & i)
End If
Next i
End Sub

Second:
Private Sub cboMainCat_Change()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("DropDowns")
Dim i As Integer

Me.cboCat.Clear
For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row ' becoz 1 is the header
If sh.Range("A" & i).Value = "Cat" Then
If sh.Range("C" & i).Value = Me.cboMainCat.Value Then
Me.cboCat.AddItem sh.Range("B" & i)
End If
End If
Next i
End Sub

Third:
Private Sub cboCat_Change()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("DropDowns")
Dim i As Integer

Me.cboSubCat.Clear
For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row ' becoz 1 is the header
If sh.Range("A" & i).Value = "Sub Category" Then
If sh.Range("C" & i).Value = Me.cboCat.Value Then
Me.cboSubCat.AddItem sh.Range("B" & i)
End If
End If
Next i
End Sub

My Data in an excel sheet:
Drop-DownsItemsParent DropDown
Main_CatHouse
Main_CatFood
Main_CatVehicle
Main_CatEntertainment
Main_CatPersonal
Main_CatMedical
Main_CatDental
Main_CatVision
CatUtilitiesHouse
CatHousehold_ItemsHouse
CatMaintenanceHouse
CatPaymentHouse
CatInsuraceHouse
CatTaxesHouse
CatMiscHouse
CatGroceriesFood
CatDiningFood
CatPaymentVehicle
CatFuelVehicle
CatInsuranceVehicle
CatTollsVehicle
CatTaxesVehicle
CatRoadside AssistanceVehicle
CatMembershipVehicle
CatRMVVehicle
CatMiscVehicle
CatRoad TripEntertainment
CatVacationEntertainment
CatLocal ActivitiesEntertainment
CatKevinPersonal
CatSaruPersonal
CatPetPersonal
CatIncome TaxPersonal
CatKevinMedical
CatSaruMedical
CatKevinDental
CatSaruDental
CatKevinVision
CatSaruVision
Sub CategoryElectricUtilities
Sub CategoryWaterUtilities
Sub CategoryTrashUtilities
Sub CategorySewerUtilities
Sub CategoryFuelUtilities
Sub CategoryOtherUtilities
Sub CategoryLaundryHousehold_Items
Sub CategoryAppliancesHousehold_Items
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try this

VBA Code:
Dim sh As Worksheet     'at the beginning of all the code

Private Sub cboMainCat_Change()
  Dim i As Long
  cboCat.Clear
  cboSubCat.Clear
  For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row
    If sh.Range("A" & i).Value = "Cat" And sh.Range("C" & i).Value = cboMainCat.Value Then
      cboCat.AddItem sh.Range("B" & i)
    End If
  Next i
End Sub

Private Sub cboCat_Change()
  Dim i As Long
  cboSubCat.Clear
  For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row
    If sh.Range("A" & i).Value = "Sub Category" And sh.Range("C" & i).Value = cboCat.Value Then
      cboSubCat.AddItem sh.Range("B" & i)
    End If
  Next i
End Sub

Private Sub UserForm_Activate()
  Set sh = ThisWorkbook.Sheets("DropDowns")
  Dim i As Long
  cboMainCat.Clear
  For i = 2 To sh.Range("A" & Rows.Count).End(xlUp).Row ' becoz 1 is the header
    If sh.Range("A" & i).Value = "Main_Cat" Then
      cboMainCat.AddItem sh.Range("B" & i)
    End If
  Next i
End Sub
 
Upvote 0
Try this

VBA Code:
Dim sh As Worksheet     'at the beginning of all the code

Private Sub cboMainCat_Change()
  Dim i As Long
  cboCat.Clear
  cboSubCat.Clear
  For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row
    If sh.Range("A" & i).Value = "Cat" And sh.Range("C" & i).Value = cboMainCat.Value Then
      cboCat.AddItem sh.Range("B" & i)
    End If
  Next i
End Sub

Private Sub cboCat_Change()
  Dim i As Long
  cboSubCat.Clear
  For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row
    If sh.Range("A" & i).Value = "Sub Category" And sh.Range("C" & i).Value = cboCat.Value Then
      cboSubCat.AddItem sh.Range("B" & i)
    End If
  Next i
End Sub

Private Sub UserForm_Activate()
  Set sh = ThisWorkbook.Sheets("DropDowns")
  Dim i As Long
  cboMainCat.Clear
  For i = 2 To sh.Range("A" & Rows.Count).End(xlUp).Row ' becoz 1 is the header
    If sh.Range("A" & i).Value = "Main_Cat" Then
      cboMainCat.AddItem sh.Range("B" & i)
    End If
  Next i
End Sub
 
Upvote 0
Hi Dante,

I made changes based on your feedback. I am still not having any luck with the 3rd combo box. This is what i have for vba code;

Option Explicit
Public enableevents As Boolean
Dim sh As Worksheet

Private Sub UserForm_Activate()
Set sh = ThisWorkbook.Sheets("DropDowns")
Dim i As Long

cboMainCat.Clear

For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row ' becoz 1 is the header
If sh.Range("A" & i).Value = "Main_Cat" Then
cboMainCat.AddItem sh.Range("B" & i)
End If
Next i
End Sub

Private Sub cboMainCat_Change()
Dim i As Long

cboCat.Clear
cboSubCat.Clear

For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row ' becoz 1 is the header
If sh.Range("A" & i).Value = "Cat" And sh.Range("C" & i).Value = cboMainCat.Value Then
cboCat.AddItem sh.Range("B" & i)
End If
Next i
End Sub

Private Sub cboCat_Change()
Dim i As Long

cboSubCat.Clear

For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row ' becoz 1 is the header
If sh.Range("A" & i).Value = "Sub Category" And sh.Range("C" & i).Value = cboCat.Value Then
cboSubCat.AddItem sh.Range("B" & i)
End If
Next i
End Sub
 
Upvote 0
Did you test my code with your examples before making changes?
 
Upvote 0
I updated my code with your code and it didn't work.
 
Upvote 0
I updated my code with your code

My code replaces all your code.
Or in what way does it not work?
According to your data that you put in your initial post, my code works well.

Test my macro with this data structure.

varios 15may2020.xlsm
ABC
1Drop-DownsItemsParent DropDown
2Main_CatHouse
3Main_CatFood
4Main_CatVehicle
5Main_CatEntertainment
6Main_CatPersonal
7Main_CatMedical
8Main_CatDental
9Main_CatVision
10CatUtilitiesHouse
11CatHousehold_ItemsHouse
12CatMaintenanceHouse
13CatPaymentHouse
14CatInsuraceHouse
15CatTaxesHouse
16CatMiscHouse
17CatGroceriesFood
18CatDiningFood
19CatPaymentVehicle
20CatFuelVehicle
21CatInsuranceVehicle
22CatTollsVehicle
23CatTaxesVehicle
24CatRoadside AssistanceVehicle
25CatMembershipVehicle
26CatRMVVehicle
27CatMiscVehicle
28CatRoad TripEntertainment
29CatVacationEntertainment
30CatLocal ActivitiesEntertainment
31CatKevinPersonal
32CatSaruPersonal
33CatPetPersonal
34CatIncome TaxPersonal
35CatKevinMedical
36CatSaruMedical
37CatKevinDental
38CatSaruDental
39CatKevinVision
40CatSaruVision
41Sub CategoryElectricUtilities
42Sub CategoryWaterUtilities
43Sub CategoryTrashUtilities
44Sub CategorySewerUtilities
45Sub CategoryFuelUtilities
46Sub CategoryOtherUtilities
47Sub CategoryLaundryHousehold_Items
48Sub CategoryAppliancesHousehold_Items
DropDowns
 
Upvote 0
Hi Dante Amor, I created a new xlsm, used your code and the data. It still didn't work. I am not sure what else I can do other than sharing my file. Let me know i can attach my file. Thank you!
 
Upvote 0
You could upload a copy of your file to a free site such www.dropbox.com or google drive. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Here is the link to the file;
 
Upvote 0

Forum statistics

Threads
1,223,250
Messages
6,171,036
Members
452,374
Latest member
keccles

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