+ Reply to Thread
Results 1 to 9 of 9

VBA code for creating dependent drop down list

Hybrid View

  1. #1
    Registered User
    Join Date
    06-25-2009
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    76

    VBA code for creating dependent drop down list

    I would like to create two drop lists, one dependent on the other. For example there will be two drop down list STATE and CITY. When I select a value from drop down list STATE, correspondingly the values of CITY drop down list must change.
    It would be nice if you could embed the code in XLS and attach it to your post rather than posting the code.
    I am using MS Office 2003

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: VBA code for creating dependent drop down list

    Do you know you can do this without VBA?

    There's some info here: http://www.contextures.com/xldataval02.html

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    06-25-2009
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    76

    Re: VBA code for creating dependent drop down list

    but I am looking for VBA...perhaps some SQL statement

  4. #4
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: VBA code for creating dependent drop down list

    Just thought I should point out that it can be done without it.

    Have a look here: http://www.xtremevbtalk.com/showpost...22&postcount=5

    Dom

  5. #5
    Registered User
    Join Date
    09-26-2008
    Location
    India
    Posts
    1

    Re: VBA code for creating dependent drop down list

    Quote Originally Posted by Domski View Post
    Just thought I should point out that it can be done without it.

    Have a look here: http://www.xtremevbtalk.com/showpost...22&postcount=5

    Dom
    Thanks Nice example

  6. #6
    Registered User
    Join Date
    06-25-2009
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    76

    Re: VBA code for creating dependent drop down list

    Quote Originally Posted by Domski View Post
    Do you know you can do this without VBA?

    There's some info here: http://www.contextures.com/xldataval02.html

    Dom
    Hi, thanks. I have two problems with this. Firstly, is that I am unable to modify the named list, or delete it. Secondly, the values don't get resetted to blank if i change my parent value in drop down list. For exampl I have selected FRUIT in my parent dropdown and APPLE in my dependent dropdown. Now if I change FRUIT to VEGETABLE, the dependant cell still shows APPLE. I want it to be resetted to blank.

    Please could you help

  7. #7
    Forum Contributor
    Join Date
    09-04-2007
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    133

    Re: VBA code for creating dependent drop down list

    Quote Originally Posted by singhabhijitkumar View Post
    Hi, thanks. I have two problems with this. Firstly, is that I am unable to modify the named list, or delete it. Secondly, the values don't get resetted to blank if i change my parent value in drop down list. For exampl I have selected FRUIT in my parent dropdown and APPLE in my dependent dropdown. Now if I change FRUIT to VEGETABLE, the dependant cell still shows APPLE. I want it to be resetted to blank.

    Please could you help
    use
    Private Sub ComboBox1_change()
        Dim t As Range
        Dim i As Integer
        Dim ws As Worksheet
        Set ws = ActiveWorkbook.Worksheets("Sheet1")
        
        ComboBox1.ShowDropButtonWhen = fmShowDropButtonWhenAlways
            
        If ComboBox1.Value = "" Then ComboBox2.Clear
        If ComboBox1.ListIndex > 0 Then ComboBox2.Clear
            If ComboBox1.ListIndex = 0 Then
                ComboBox2.Clear
                ComboBox2.Value = ""
            ElseIf ComboBox1.ListIndex = 1 Then
                ComboBox2.Clear
                For Each tower In ws.Range("Range1")
                    With Me.ComboBox2
                        .AddItem t.Value
                        .List(.ListCount - 1, 1) = t.Offset(0, 1).Value
                    End With
                Next t
            ElseIf ComboBox1.ListIndex = 2 Then
                ComboBox2.Clear
                For Each tower In ws.Range("Range2")
                    With Me.ComboBox2
                        .AddItem t.Value
                        .List(.ListCount - 1, 1) = t.Offset(0, 1).Value
                    End With
                Next t
            ElseIf ComboBox1.ListIndex = 3 Then
               ComboBox2.Clear
                For Each tower In ws.Range("Range3")
                    With Me.ComboBox2
                        .AddItem t.Value
                        .List(.ListCount - 1, 1) = t.Offset(0, 1).Value
                    End With
                Next t
            End If
    End Sub
    Hope this helps...
    Last edited by skonduru; 05-26-2010 at 08:31 AM.

  8. #8
    Registered User
    Join Date
    06-25-2009
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    76

    Re: VBA code for creating dependent drop down list

    Quote Originally Posted by Domski View Post
    Do you know you can do this without VBA?

    There's some info here: http://www.contextures.com/xldataval02.html

    Dom
    Hi, thanks. I have two problems with this. Firstly, is that I am unable to modify the named list, or delete it. Secondly, the values don't get resetted to blank if i change my parent value in drop down list. For exampl I have selected FRUIT in my parent dropdown and APPLE in my dependent dropdown. Now if I change FRUIT to VEGETABLE, the dependant cell still shows APPLE. I want it to be resetted to blank.

    Please could you help

  9. #9
    Forum Contributor
    Join Date
    09-04-2007
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    133

    Re: VBA code for creating dependent drop down list

    Follow the link below to find the related post...

    http://www.excelforum.com/excel-prog...ist-boxes.html

    Its easy to follow and helps alot...


    Small suggestion here, if u want to add small data use below code..

     With ComboBox2
                .List = Array("Japan", "China", "Korea", "Taiwan", "Aus/ANZ", "Hong Kong", "Singapore")
            End With
    otherwise, if u want to add data from the spreadsheet range use below for loop to add data to the combo box
            For Each tower In ws.Range("TowerNum_Type1r")
                With Me.cmbTowerNumber
                    .AddItem tower.Value
                    .List(.ListCount - 1, 1) = tower.Offset(0, 1).Value
                End With
            Next tower

    Regards,

    SK
    Last edited by skonduru; 05-25-2010 at 10:25 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