Combobox in word form

MrTball

New Member
Joined
Oct 23, 2015
Messages
11
Hi,

Noob here, I am having problems with combo box showing answers depending on previous combo box, here is my code...

It works when selecting from combo box6 to populate combobox3 but combobox5 dues not seem correct, I may be missing something easy but like I said I am a NOOB!!!

Any help would be appreciated

Code:
Private Sub UserForm_Initialize()

    

    ComboBox2.Clear
    Call ComboBox2.AddItem("1")
    Call ComboBox2.AddItem("2")
    Call ComboBox2.AddItem("3")
    Call ComboBox2.AddItem("4")
    
        ComboBox6.Clear
    Call ComboBox6.AddItem("Mechanical Engineering")
    Call ComboBox6.AddItem("Engineering")
    Call ComboBox6.AddItem("Science")
   

Private Sub ComboBox6_Change()
Dim index As Integer
index = ComboBox6.ListIndex

ComboBox3.Clear

Select Case index
    
    Case Is = 0
        With ComboBox3
            .AddItem "Mechanical Measurement and Inspection Techniques"
            .AddItem "Applications Of Computer Numerical Control in Engineering"
            .AddItem "Computer Aided Manufacturing"
            .AddItem "Applications of Computer Numerical Control in Engineering & Computer Aided Manufacture"
            End With
        Case Is = 1
           With ComboBox3
            .AddItem "welding"
            .AddItem "Other stuff"
            .AddItem "Not sure"
            End With
        Case Is = 2
           With ComboBox3
            .AddItem "Physics"
            .AddItem "Biology"
            .AddItem "Chemistry"
            End With
            
           End Select
End Sub

Private Sub ComboBox3_Change()
Dim index As Integer
index = ComboBox3.ListIndex

ComboBox5.Clear

Select Case index
    
    Case Is = 0
        With ComboBox5
            .AddItem "Linear Angular and Comparative Measurement"
            .AddItem "Limits Fits and Guaging"
            .AddItem "Statistical Process Control and Process Capability"
            End With
        Case Is = 1
           With ComboBox5
            .AddItem "CNC Principles and Machines"
            .AddItem "Component Specifications and Operational Plans for Manufacture"
            .AddItem "Part Programming and Manufacturing Components"
            End With
        Case Is = 2
            With ComboBox5
            .AddItem "Using CAM and Smart Systems in Engineering"
            .AddItem "CAD Cam Interfacing"
            .AddItem "Industrial Robots and Engineering Systems"
            End With
        Case Is = 3
            With ComboBox5
            .AddItem "Using CAD/CAM Software to Manufacture Components"
            End With
        Case Is = 4
            With ComboBox5
            .AddItem "force"
            .AddItem "Organs"
            .AddItem "Chemicals"
            End With
    End Select
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
It works when selecting from combo box6 to populate combobox3 but combobox5 dues not seem correct, I may be missing something easy but like I said I am a NOOB!
It's a bit difficult to advise on 'dues not seem correct' when you give no indication of what 'correct' is. That said, you code could be improved:
Code:
Private Sub UserForm_Initialize()
With ComboBox2
  .Clear
  .AddItem "1"
  .AddItem "2"
  .AddItem "3"
  .AddItem "4"
End With
With ComboBox6
  .Clear
  .AddItem "Mechanical Engineering"
  .AddItem "Engineering"
  .AddItem "Science"
End With
End Sub

Private Sub ComboBox6_Change()
Dim index As Long
index = ComboBox6.ListIndex
With ComboBox3
  .Clear
  Select Case index
    Case Is = 0
    .AddItem "Mechanical Measurement and Inspection Techniques"
    .AddItem "Applications Of Computer Numerical Control in Engineering"
    .AddItem "Computer Aided Manufacturing"
    .AddItem "Applications of Computer Numerical Control in Engineering & Computer Aided Manufacture"
    Case Is = 1
    .AddItem "welding"
    .AddItem "Other stuff"
    .AddItem "Not sure"
    Case Is = 2
    .AddItem "Physics"
    .AddItem "Biology"
    .AddItem "Chemistry"
  End Select
End With
End Sub

Private Sub ComboBox3_Change()
Dim index As Long
index = ComboBox3.ListIndex
With ComboBox5
  .Clear
  Select Case index
    Case Is = 0
      .AddItem "Linear Angular and Comparative Measurement"
      .AddItem "Limits Fits and Guaging"
      .AddItem "Statistical Process Control and Process Capability"
    Case Is = 1
      .AddItem "CNC Principles and Machines"
      .AddItem "Component Specifications and Operational Plans for Manufacture"
      .AddItem "Part Programming and Manufacturing Components"
    Case Is = 2
      .AddItem "Using CAM and Smart Systems in Engineering"
      .AddItem "CAD Cam Interfacing"
      .AddItem "Industrial Robots and Engineering Systems"
    Case Is = 3
      .AddItem "Using CAD/CAM Software to Manufacture Components"
    Case Is = 4
      .AddItem "force"
      .AddItem "Organs"
      .AddItem "Chemicals"
  End Select
End With
End Sub
 
Last edited:
Upvote 0
Hi, thanks for the reply.

Sorry about being so vague, but I am struggling with this....

So what I want is in combobox6 to select one of the three to give me different options for combobox3, then depending on combobox3 answer will give different options for combobox5.....

however when I select say science I only get the options as if I selected the first option in combobox6....

So if I select science I get the options for physics Biology and Chemistry, but box 5 has answers as if I selected Mechanical Engineering and then one of the options from that set....

Again any help is appreciated.
 
Upvote 0
If your combobox 5 options depend on the combination of choices made in comboboxes 6 & 3, you need code like:
Code:
Dim IdxA As Long, IdxB As Long

Private Sub UserForm_Initialize()
With ComboBox2
  .AddItem "1"
  .AddItem "2"
  .AddItem "3"
  .AddItem "4"
End With
With ComboBox6
  .AddItem "Mechanical Engineering"
  .AddItem "Engineering"
  .AddItem "Science"
End With
End Sub

Private Sub ComboBox6_Change()
IdxA = ComboBox6.ListIndex
With ComboBox3
  .Clear
  Select Case IdxA
    Case Is = 0
    .AddItem "Mechanical Measurement and Inspection Techniques"
    .AddItem "Applications Of Computer Numerical Control in Engineering"
    .AddItem "Computer Aided Manufacturing"
    .AddItem "Applications of Computer Numerical Control in Engineering & Computer Aided Manufacture"
    Case Is = 1
    .AddItem "welding"
    .AddItem "Other stuff"
    .AddItem "Not sure"
    Case Is = 2
    .AddItem "Physics"
    .AddItem "Biology"
    .AddItem "Chemistry"
  End Select
End With
End Sub

Private Sub ComboBox3_Change()
IdxB = ComboBox3.ListIndex
With ComboBox5
  .Clear
  Select Case IdxA
    Case Is = 0
      Select Case IdxB
        Case Is = 0
          .AddItem "IdxA = 0 IdxB = 0 Item 1"
          .AddItem "IdxA = 0 IdxB = 0 Item 2"
          .AddItem "IdxA = 0 IdxB = 0 Item 3"
        Case Is = 1
          .AddItem "IdxA = 0 IdxB = 1 Item 1"
          .AddItem "IdxA = 0 IdxB = 1 Item 2"
          .AddItem "IdxA = 0 IdxB = 1 Item 3"
        Case Is = 2
          .AddItem "IdxA = 0 IdxB = 2 Item 1"
          .AddItem "IdxA = 0 IdxB = 2 Item 2"
          .AddItem "IdxA = 0 IdxB = 2 Item 3"
        Case Is = 3
          .AddItem "IdxA = 0 IdxB = 3 Item 1"
          .AddItem "IdxA = 0 IdxB = 3 Item 2"
          .AddItem "IdxA = 0 IdxB = 3 Item 3"
        Case Is = 4
          .AddItem "IdxA = 0 IdxB = 4 Item 1"
          .AddItem "IdxA = 0 IdxB = 4 Item 2"
          .AddItem "IdxA = 0 IdxB = 4 Item 3"
      End Select
    Case Is = 1
        Case Is = 0
          .AddItem "IdxA = 1 IdxB = 0 Item 1"
          .AddItem "IdxA = 1 IdxB = 0 Item 2"
          .AddItem "IdxA = 1 IdxB = 0 Item 3"
        Case Is = 1
          .AddItem "IdxA = 1 IdxB = 1 Item 1"
          .AddItem "IdxA = 1 IdxB = 1 Item 2"
          .AddItem "IdxA = 1 IdxB = 1 Item 3"
        Case Is = 2
          .AddItem "IdxA = 1 IdxB = 2 Item 1"
          .AddItem "IdxA = 1 IdxB = 2 Item 2"
          .AddItem "IdxA = 1 IdxB = 2 Item 3"
        Case Is = 3
          .AddItem "IdxA = 1 IdxB = 3 Item 1"
          .AddItem "IdxA = 1 IdxB = 3 Item 2"
          .AddItem "IdxA = 1 IdxB = 3 Item 3"
        Case Is = 4
          .AddItem "IdxA = 1 IdxB = 4 Item 1"
          .AddItem "IdxA = 1 IdxB = 4 Item 2"
          .AddItem "IdxA = 1 IdxB = 4 Item 3"
      End Select
    Case Is = 2
        Case Is = 0
          .AddItem "IdxA = 2 IdxB = 0 Item 1"
          .AddItem "IdxA = 2 IdxB = 0 Item 2"
          .AddItem "IdxA = 2 IdxB = 0 Item 3"
        Case Is = 1
          .AddItem "IdxA = 2 IdxB = 1 Item 1"
          .AddItem "IdxA = 2 IdxB = 1 Item 2"
          .AddItem "IdxA = 2 IdxB = 1 Item 3"
        Case Is = 2
          .AddItem "IdxA = 2 IdxB = 2 Item 1"
          .AddItem "IdxA = 2 IdxB = 2 Item 2"
          .AddItem "IdxA = 2 IdxB = 2 Item 3"
        Case Is = 3
          .AddItem "IdxA = 2 IdxB = 3 Item 1"
          .AddItem "IdxA = 2 IdxB = 3 Item 2"
          .AddItem "IdxA = 2 IdxB = 3 Item 3"
        Case Is = 4
          .AddItem "IdxA = 2 IdxB = 4 Item 1"
          .AddItem "IdxA = 2 IdxB = 4 Item 2"
          .AddItem "IdxA = 2 IdxB = 4 Item 3"
      End Select
  End Select
End With
End Sub
Note how the ComboBox3_Change code tests the indices of both itself and combobox 6.
 
Upvote 0
Thanks Macropod,

I am out of office at the moment but it looks good and I will give this a try tomorrow afternoon and get back to you.

Thank you for you time and effort...

Regards
MrTball
 
Upvote 0
Hi, sorry for the late reply, I have had a family crisis over the holidays and have only just returned to work last week.

I have tested the code above but when selecting combobox5 I get the correct list but also other lists from other subjects that I should not be able to see.


Code:
Private Sub UserForm_Initialize()

    
Dim IdxA As Long, IdxB As Long

With ComboBox2
  .AddItem "1"
  .AddItem "2"
  .AddItem "3"
  .AddItem "4"
End With
With ComboBox6
  .AddItem "Mechanical Engineering"
  .AddItem "Engineering"
  .AddItem "Science"
End With
End Sub

Private Sub ComboBox6_Change()
IdxA = ComboBox6.ListIndex
With ComboBox3
  .Clear
  Select Case IdxA
    Case Is = 0
    .AddItem "Mechanical Measurement and Inspection Techniques"
    .AddItem "Applications Of Computer Numerical Control in Engineering"
    .AddItem "Computer Aided Manufacturing"
    .AddItem "Applications of Computer Numerical Control in Engineering & Computer Aided Manufacture"
    Case Is = 1
    .AddItem "welding"
    .AddItem "Other stuff"
    .AddItem "Not sure"
    Case Is = 2
    .AddItem "Physics"
    .AddItem "Biology"
    .AddItem "Chemistry"
  End Select
End With
End Sub

Private Sub ComboBox3_Change()
IdxB = ComboBox3.ListIndex
With ComboBox5
  .Clear
  Select Case IdxA
    Case Is = 0
      Select Case IdxB
        Case Is = 0
      .AddItem "Linear Angular and Comparative Measurement"
      .AddItem "Limits Fits and Guaging"
      .AddItem "Statistical Process Control and Process Capability"
        Case Is = 1
      .AddItem "CNC Principles and Machines"
      .AddItem "Component Specifications and Operational Plans for Manufacture"
      .AddItem "Part Programming and Manufacturing Components"
        Case Is = 2
      .AddItem "Using CAM and Smart Systems in Engineering"
      .AddItem "CAD Cam Interfacing"
      .AddItem "Industrial Robots and Engineering Systems"
        Case Is = 3
    .AddItem "Applications of Computer Numerical Control in Engineering & Computer Aided Manufacture"

      End Select
      Select Case IdxB
    Case Is = 1
        Case Is = 0
          .AddItem "Weld 1"
          .AddItem "Weld 1 2"
          .AddItem "Weld 1 3"
        Case Is = 1
          .AddItem "Other assingment 1"
          .AddItem "Other assingment 2"
          .AddItem "Other assingment 3"
        Case Is = 2
          .AddItem "Not sure 1"
          .AddItem "Not sure 2"
          .AddItem "Not sure 3"

      End Select
      Select Case IdxB
    Case Is = 2
        Case Is = 0
          .AddItem "Physics 1"
          .AddItem "Physics 2"
          .AddItem "Physics 3"
        Case Is = 1
          .AddItem "Biology 1"
          .AddItem "Biology 2"
          .AddItem "Biology 3"
        Case Is = 2
          .AddItem "Chemistry 1"
          .AddItem "Chemistry 2"
          .AddItem "Chemistry 3"

      End Select
  End Select
End With
End Sub
 
Upvote 0
There is a reason I put:
Dim IdxA As Long, IdxB As Long
before:
Private Sub UserForm_Initialize()
You've swapped the order, which breaks the code...
 
Upvote 0
Lol, yep that was the problem...

Thank you sooooo much for your help this is now working how I wanted it to
 
Upvote 0
Explanation - By putting:
Dim IdxA As Long, IdxB As Long
before:
Private Sub UserForm_Initialize()
the variables are available to all code routines, so they can use and/or update them. If you reverse the order, the variables are only available to the UserForm_Initialize routine and the other routines never get to use or update them.

Hint: It's always good to use Option Explicit at the top of a code module (the VBE can be configured to add this to new modules automatically), so that (amongst other things) misnamed & missing variables will be trapped.
 
Upvote 0

Forum statistics

Threads
1,226,116
Messages
6,189,055
Members
453,523
Latest member
Don Quixote

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