Dropdown lists in excel 2007

Solved/Closed
Gouws Posts 45 Registration date Sunday February 7, 2010 Status Member Last seen April 15, 2012 - Feb 19, 2010 at 12:28 AM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - Feb 27, 2013 at 05:56 PM
Hello,
I want to create dropdown lists in excel, it should work as follow:In cell2,1 must be a drop down list with 4 names (e.g.Mill,Comp,Loads,Blending), if I select say Mill then in cell2,2 a dropdown list with all the mill parts must appear, same for the other if I select one of the other names their parts list must appear.Can anyone show me how to do this?

TX
G

6 responses

rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Feb 19, 2010 at 04:57 AM
Assumptions
1. You are not going to sue me it any thing goes wrong
2. You have a backup copy of the file


This is how I would approach
1. On a new sheet (let say sheet2) in Cells A1, B1, C1 and D1 enter values Mill, Comp, Loads, Blending respectively
2. Put the values of Mill, Comp, Loads and Blending under its respective column heading (make sure no blank line)
3. Go to Formula Tab on menu
4. Select Cells A1:D1
5. Click on define Name
6. Enter the name, lets say "MyMainList" and click Ok
7. Select all the values in col A, starting from A2 and click on name, give it name "Mill"
8. Select all the values in col B, starting from B2 and click on name, give it name "Comp"
9. Select all the values in col C, starting from C2 and click on name, give it name "Loads"
10. Select all the values in col D, starting from D2 and click on name, give it name "Blending "
11. Go to main sheet (lets say sheet1) and select cell A2
12. Go to Data tab on menu and click on Data Validation
13. Click on the drop down titled "Allow" and choose List
14. In Source box write =MyMainList
15. Optionally go to input tab and error tab to enter messages for that
16. Press ALT+F11
17. Double click on "sheet1" in the project explorer
18. Paste code.

Private Sub Worksheet_Change(ByVal Target As Range) 
'Target is the cell or a range of cells whose values were changed
' and that triggered the change. Excel itself will handles this part.
' This code is written with assumption that at a time only
' one cell would be changed

    ' if the changed column is not column A, then we are exiting
    If Target.Column <> 1 Then Exit Sub 

    ' we are disabling excel ability to react when the cell or range is 
    ' changed. It will be enabled later on. This prevents cyclic or
    ' recursive calls     
    Application.EnableEvents = False 
     
    Dim mainsrc As String 
    Dim subsrc As String 
     
    Dim lThisRow As Long 
    ' find which row was modifed that triggered this function to be called
    lThisRow = Target.Row 
    
    ' Check if the value in column A on the row that was changed is ""
    If Cells(lThisRow, 1) = "" Then 

        ' since the value in column A is blank, we are deleting any 
        ' validation from Column B on the same row
        Cells(lThisRow, 2).Validation.Delete 

        ' goto to a label "exit_sub"
        GoTo exit_sub 
    End If 
     

    mainsrc = Cells(lThisRow, 1).Value 
     
    On Error Resume Next 
    'attempting to read what is the current validation formula in cell in
   ' column B of the changed row
    subsrc = Cells(lThisRow, 2).Validation.Formula1 
    On Error GoTo 0 
    
    ' check if the current validation in the column B is same as one that
    ' is needed now due to change in the value of column 1
    If ("=" & subsrc = mainsrc) Then 
        ' the validation I n column B is for same major category
        ' no need to reset validation
        GoTo exit_sub 
    End If 
  
  ' since the major category was changed, we reset any selected value
  ' in column B of the changed row to ""    
  Cells(lThisRow, 2) = "" 

  ' putting the new validation in B
  With Cells(lThisRow, 2).Validation 
        .Delete 
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=" & mainsrc 
        .IgnoreBlank = True 
        .InCellDropdown = True 
        .InputTitle = "This is my Input Title" 
        .ErrorTitle = "Oops Error" 
        .InputMessage = "Select a Value" 
        .ErrorMessage = "Not a valid value" 
        .ShowInput = True 
        .ShowError = True 
    End With 
     

' a label to allow for "Goto" statement
exit_sub: 
     
    ' re-enabling the ability for excel to react to events/triggers
    Application.EnableEvents = True 

End Sub

19. Post Feedback
20. Send me the money :p
9
Hi rivisa1, thank it's working
G
0
Cool, I thought this is not possible; it could be easily using Ajax!! But you have done here just like that! ;-) Many Thanks rizvisa
0