+ Reply to Thread
Results 1 to 4 of 4

ComboBox Linage

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-24-2007
    Location
    Southampton, UK
    Posts
    137
    Try the code below in the Change event of the first combo box.

    Private Sub ComboBox1_Change()
    Dim rng As Range
    With ComboBox1
    Set rng = Range(.ListFillRange)
    ComboBox2.ListFillRange = Range(rng.Offset(IIf(.ListIndex > -1, .ListIndex, 0), 0), rng.Cells(rng.Rows.Count, rng.Columns.Count)).Address
    End With
    If ComboBox2.ListIndex = -1 Then ComboBox2.Value = Null
    End Sub
    This copies the fill range used by the 1st combo box to the 2nd one, but starting from the value selected in the 1st combo box. Any value already in the 2nd box is retained if it is still valid, otherwise it is deleted.

    If no value is selected in the 1st box, the 2nd box references the entire fill range of the 1st box.

  2. #2
    Registered User
    Join Date
    03-17-2005
    Posts
    19
    So, would I need to create 35 of these?

    Also, s there some way to do this in the Forms menu of Excel rather than through MACRO?

  3. #3
    Forum Contributor
    Join Date
    01-24-2007
    Location
    Southampton, UK
    Posts
    137
    Apologies, I had assumed that your comboboxes had been set-up through the Control Toolbox menu items.

    Using the Forms menu, comboboxes and the like become Excel Shapes, and so need a different approach when programming them.

    First, you will need to know the names of the two comboboxes. This is simply done by left-clicking on each in turn whilst holding down the [Ctrl] key. Their names will appear in the names area in the top left of the screen (where you usually see the cell references or names). Note these names down (being careful to note if there are any spaces).

    Then, on the first combobox, left-click whilst holding down the [Ctrl] key and select the Edit Code item from the Forms menu. This should bring you into the Visual Basic windows and you should be presented with a couple of lines somethng like the following -

    Sub DropDown1_Change()

    End Sub


    Between these two lines you can copy & paste the following code....

    On Error Resume Next
    Dim rng As Range
    
    Const ComboOne$ = "Drop Down 1"
    Const ComboTwo$ = "Drop Down 2"
    
    With ActiveSheet.Shapes(ComboOne$).OLEFormat.Object
    Set rng = Range(.ListFillRange)
    ActiveSheet.Shapes(ComboTwo$).OLEFormat.Object.ListFillRange = _
        Range(rng.Offset(.ListIndex - 1, 0), rng.Cells(rng.Rows.Count, rng.Columns.Count)).Address
    End With
    ActiveSheet.Shapes(ComboTwo$).OLEFormat.Object.ListIndex = 0
    The two names highlighted in red above need to be changed to whatever the names of your comboboxes are.

    Whenever the 1st combobox is changed, the available items in the 2nd box will start from the value of the 1st box. Any value in the 2nd box will be deleted.

    This short program only has to be set-up once against the 1st combobox (no code is needed for the 2nd combobox). The code assumes that the 1st combobox has already been set-up with the input range for the lookup values.
    Last edited by Loz; 04-25-2007 at 09:53 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1