Userform to show only specific data in combobox dependant on other combobox selection

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,736
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I have a userform with various comboboxes.
Ther 2 comboboxes in question are as follows.

ComboBox3 of which is MAKE comprising of "HONDA" "KAWASAKI" "YAMAHA" & are the values in drop down list
This is Table 1

ComboBox8 of which is ORIGINAL PART NUMBERS comprising of ALL part number for all the makes mentioned above currently say 20 of them
Currently this is Table 6

So example of what i would like to do.
If HONDA is selected in ComboBox3 then only show the honda part numbers in ComboBox8

So i assume that i need new tables for each make so i have now done that.
HONDA is Table 9
KAWASAKI is Table 10
YAMAHA is Table 11

Later i will be adding more but for now can you advise how i write the code for ComboBox8

Example
If HONDA is selected only show Table 9 results
ELSE
If KAWASAKI is selected only show Table 10 result
ELSE
If YAMAHA is selected only show Table 11 result


Many thanks
 
This was very good & wondering if you could go one step further please.

Using the same Combobox3 values can we ALSO populate Combobox4 with MODELS

Tables will be names like so,
HondaModels
KawasakiModels
YamahaModels

Yes you can - just create tables similar to my example using your suggested naming convention & name the worksheet appropriately (Model Tables)

the use updated code

Rich (BB code):
Private Sub ComboBox3_Change()
    Dim wsPartsTables      As Worksheet
    Dim wsModelTables      As Worksheet
    Dim tbl(1 To 2)        As ListObject
    Dim whichtable         As String
      
    'make
    whichtable = Me.ComboBox3.Value
    If Len(whichtable) = 0 Then Exit Sub
  
      'set object variable to combobox parts tables worksheet
    Set wsPartsTables = ThisWorkbook.Worksheets("Parts Tables")
  
     'set object variable to combobox model tables worksheet
    Set wsModelTables = ThisWorkbook.Worksheets("Model Tables")
  
    Set tbl(1) = wsPartsTables.ListObjects(whichtable & "table")
  
    Set tbl(2) = wsModelTables.ListObjects(whichtable & "Models")
  
    'parts
     With Me.ComboBox8
        .RowSource = ""
        .List = tbl(1).DataBodyRange.Value
     End With
   
     'models
     With Me.ComboBox4
        .RowSource = ""
        .List = tbl(2).DataBodyRange.Value
     End With
  
End Sub

Personally, I would name the parts table worksheet "Parts Tables" as it is more representative as to it purpose than "Database" - I would also suggest that you consider updating your combobox names from their default names to something more meaningful like

cboMake
cboModel
cboParts


but your choice - just update combobox & worksheet names as appropriate.

Dave
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I have applied the code & i see Could not set the list property,invalid property array
I debug & see the line of code below in red shown.

Im using ComboBox 4 & 8
Both tables are on worksheet DATABASE
Tables are like so HondaTable & HondaModel

Rich (BB code):
Private Sub ComboBox3_Change()
    Dim wsPartsTables      As Worksheet
    Dim wsModelTables      As Worksheet
    Dim tbl(1 To 2)        As ListObject
    Dim whichtable         As String
      
    'make
    whichtable = Me.ComboBox3.Value
    If Len(whichtable) = 0 Then Exit Sub
  
      'set object variable to combobox parts tables worksheet
    Set wsPartsTables = ThisWorkbook.Worksheets("DATABASE")
  
     'set object variable to combobox model tables worksheet
    Set wsModelTables = ThisWorkbook.Worksheets("DATABASE")
  
    Set tbl(1) = wsPartsTables.ListObjects(whichtable & "table")
  
    Set tbl(2) = wsModelTables.ListObjects(whichtable & "model")
  
    'parts
     With Me.ComboBox8
        .RowSource = ""
        .List = tbl(1).DataBodyRange.Value
     End With
   
     'models
     With Me.ComboBox4
        .RowSource = ""
        .List = tbl(2).DataBodyRange.Value
     End With
  
End Sub
 
Upvote 0
See here

TEST.xlsm
KLMNOPQRSTUVWXYZ
1HONDA PNKAWASAKI PNSUZUKI TABLEYAMAHA PNPIAGGIO PNAPRILIA PNHONDA MKAWASAKI MSUZUKI MYAMAHA MPIAGGIO MAPRILLIA M
235121-MAT-E0027008061537146-26D10-0005SL-82511-08-00CB 1000ZZR 1400GSXR 1000 RRMT09EF
335121-MBW-60137146-33E00-000CB 1300GSXR 600R1
435121-MCA-811CB 500 FST 1300XJR 1300
535121-MCJ-D00CB 500 XRF 900XVS 1300
635121-MEH-631CB 600 F
735121-MFJ-D00CB 600 N
835121-MFL-641CB 650 F
935121-MFN-D01CB 900 F
1035121-MGH-641CBF 1000
1135121-MGP-D01CBF 600
1235121-MGP-D63CBR 1000
1335121-MGS-A31CBR 1000 RR
1435121-MJE-D02CBR 1100 XX
1535121-MJG-641CBR 600 F
1672147-MCA-305CBR 600 R
1772147-MCA-315CBR 600 RR
1872147-MCA-671CBR 650 F
1972147-MJK-305CBR 650 R
2072147-MKJ-326CBR 900 RR
21CBR 954
22GOLD WING
23NC 700 S
24NC 700 X
25NC 750
26NC 750 X
27NT 650
28NT 700
29SILVER WING
30SH 300 A
31ST 1300
32VALKYRIE
33VFR 1200
34VFR 800
35VTR 1000
36X11
37XL 650
38XL 700
39AFRICA TWIN
DATABASE
 
Upvote 0
sheet looks like you are using a ranges

Can I suggest that you follow my suggested guidance - Insert a new sheet & name it Model Tables

then create your tables in following format

07-01-2023.xls
ABCDE
1HondaKAWASAKIYAMAHA
2HON ModelKW ModelYAM Model
3HON ModelKW ModelYAM Model
4HON ModelKW ModelYAM Model
5HON ModelKW ModelYAM Model
6HON ModelKW ModelYAM Model
7HON ModelKW ModelYAM Model
8HON ModelKW ModelYAM Model
Model Tables


Tables should be named as per you naming suggestion

HondaModels
KawasakiModels
YamahaModels

Dave
 
Upvote 0
Well i obviously cant follow this now for some reason.

I have now created two new worksheets.
MODEL TABLES & PART NUMBER TABLES.

My code if like so but i now keep getting subscript out of range.
What am i doing wrong please

Rich (BB code):
Private Sub ComboBox3_Change()
    Dim wsPartsTables      As Worksheet
    Dim wsModelTables      As Worksheet
    Dim tbl(1 To 2)        As ListObject
    Dim whichtable         As String
      
    'make
    whichtable = Me.ComboBox3.Value
    If Len(whichtable) = 0 Then Exit Sub
  
      'set object variable to combobox parts tables worksheet
    Set wsPartsTables = ThisWorkbook.Worksheets("MODEL TABLES")
  
     'set object variable to combobox model tables worksheet
    Set wsModelTables = ThisWorkbook.Worksheets("PART NUMBER TABLES")
  
    Set tbl(1) = wsPartsTables.ListObjects(whichtable & "table")
  
    Set tbl(2) = wsModelTables.ListObjects(whichtable & "table")
  
    'parts
     With Me.ComboBox8
        .RowSource = ""
        .List = tbl(1).DataBodyRange.Value
     End With
   
     'models
     With Me.ComboBox4
        .RowSource = ""
        .List = tbl(2).DataBodyRange.Value
     End With
  
End Sub
 
Upvote 0
Well i obviously cant follow this now for some reason.

I have now created two new worksheets.
MODEL TABLES & PART NUMBER TABLES.

My code if like so but i now keep getting subscript out of range.
What am i doing wrong please

Rich (BB code):
Private Sub ComboBox3_Change()
    Dim wsPartsTables      As Worksheet
    Dim wsModelTables      As Worksheet
    Dim tbl(1 To 2)        As ListObject
    Dim whichtable         As String
     
    'make
    whichtable = Me.ComboBox3.Value
    If Len(whichtable) = 0 Then Exit Sub
 
      'set object variable to combobox parts tables worksheet
    Set wsPartsTables = ThisWorkbook.Worksheets("MODEL TABLES")
 
     'set object variable to combobox model tables worksheet
    Set wsModelTables = ThisWorkbook.Worksheets("PART NUMBER TABLES")
 
    Set tbl(1) = wsPartsTables.ListObjects(whichtable & "table")
 
    Set tbl(2) = wsModelTables.ListObjects(whichtable & "table")
 
    'parts
     With Me.ComboBox8
        .RowSource = ""
        .List = tbl(1).DataBodyRange.Value
     End With
  
     'models
     With Me.ComboBox4
        .RowSource = ""
        .List = tbl(2).DataBodyRange.Value
     End With
 
End Sub

error suggests code cannot find worksheet or table

If having difficulty setting this up place copy of your workbook with dummy data in a filesharing site like dropbox & provide a link to it & will have a look.

Dave
 
Upvote 0
Hi,
looks like you got yourself in to a bit of a pickle referencing the correct table worksheets

this works with sample workbook you posted

VBA Code:
Private Sub ComboBox3_Change()
    Dim wsPartsTables          As Worksheet
    Dim wsModelTables          As Worksheet
    Dim tbl(1 To 2)            As ListObject
    Dim whichtable             As String
     
    'make
    whichtable = Me.ComboBox3.Value
    If Len(whichtable) = 0 Then Exit Sub
 
      'set object variable to combobox parts tables worksheet
    Set wsPartsTables = ThisWorkbook.Worksheets("Parts Tables")
 
     'set object variable to combobox model tables worksheet
    Set wsModelTables = ThisWorkbook.Worksheets("Model Tables")
 
    Set tbl(1) = wsPartsTables.ListObjects(whichtable & "Table")
 
    Set tbl(2) = wsModelTables.ListObjects(whichtable & "Model")
 
    'parts
     With Me.ComboBox8
        .RowSource = ""
        .List = tbl(1).DataBodyRange.Value
     End With
  
     'models
     With Me.ComboBox4
        .RowSource = ""
        .List = tbl(2).DataBodyRange.Value
     End With
 
End Sub

If you still get out of range errors check each the names of your tables to ensure they match naming convention

Dave
 
Upvote 0
Ived used the code you have supplied above but i still get the same issue.

Ive suppl;ied image of my named worksheets

So were on the same page the worksheet names refer to this piece opf code here correct ?

Rich (BB code):
      'set object variable to combobox parts tables worksheet
    Set wsPartsTables = ThisWorkbook.Worksheets("Parts Tables")
  
     'set object variable to combobox model tables worksheet
    Set wsModelTables = ThisWorkbook.Worksheets("Model Tables")

If so i even renamed them to A & B but still the same
 

Attachments

  • 4902.jpg
    4902.jpg
    16.4 KB · Views: 5
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

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