Drop down list - adding a new field below?

Csibész

New Member
Joined
Nov 17, 2005
Messages
10
Hello everyone,

I'm clueless how to make this work...
The following shows in detail how I "created" a drop down list in Excel 2010:

DLtest.jpg


Left: In one sheet (‘DLoptions′) I entered the drop down list options.
Middle: ‘Sheet1′ I specified the locations for the drop down list fields.
Right: Shows what I'd like to accomplish: after (or before) a selection is made from the drop down list, a new field with the same options should be inserted.

It is not known ahead of time how many fields will be needed in any of 'My (xxx) List' groups. The only requirement is that once an option is selected another opportunity for selection be made available in the cell below. Within a group no duplication is allowed (e.g. there can't be two 'Item 1' under 'My List').

Is there a way to accomplish the task with or preferably without a macro?

Thanks in advance for a solution.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I don't think this is possible without macro. Either macro or manually :)
 
Upvote 0
I did not follow your sheet 100% trying not to type long ?. I have also added a keyword EndList to simplify my program in identifying range of each validation list.

The macro is a Worksheet_SelectionChange event triggered. So you need to copy the code under Sheet1, not in normal macro module. Here is how my SHeet1 Looks like. Sheet2 follow yours
Shifting Group.xlsm
ABC
1ListGrp1
2
3ListGrp2
4
5ListGrp3
6
7EndList
8
9
Sheet1

Shifting Group.xlsm
ABCD
1Group1Group2Group3
2Item1Piece1Unit1
3Item2Piece2Unit2
4Item3Piece3Unit3
5Item4Piece4Unit4
6Item5Piece5Unit5
7
8
DLoptions

Here's the code. Not sure if 100% bug free though
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, Range("A2:A500")) Is Nothing Then

    Dim strList As String
    Dim ValCount As Long
    Dim cell As Range, rngValidation As Range, nxTarget As Range
    Dim rngTitle1 As Range, rngTitle2 As Range, rngTitle3 As Range, rngEnd As Range
    Dim rngListGrp1 As Range, rngListGrp2 As Range, rngListGrp3 As Range
    Dim rngGrp1 As Range, rngGrp2 As Range, rngGrp3 As Range
    Dim Cond As Boolean
    Dim dGrp1 As Object, dGrp2 As Object, dGrp3 As Object
    Dim ws2 As Worksheet
    
    Application.ScreenUpdating = False
    Set ws2 = ActiveWorkbook.Sheets("DLoptions")

    Set rngListGrp1 = ws2.Range("A2", ws2.Cells(Rows.Count, "A").End(xlUp))
    Set rngListGrp2 = ws2.Range("B2", ws2.Cells(Rows.Count, "B").End(xlUp))
    Set rngListGrp3 = ws2.Range("C2", ws2.Cells(Rows.Count, "C").End(xlUp))

    Set dGrp1 = CreateObject("Scripting.Dictionary")
    Set dGrp2 = CreateObject("Scripting.Dictionary")
    Set dGrp3 = CreateObject("Scripting.Dictionary")

    Set rngValidation = Range("A1", Cells(Rows.Count, "A").End(xlUp))

    Set rngTitle1 = rngValidation.Find(What:="ListGrp1", LookAt:=xlWhole)
    Set rngTitle2 = rngValidation.Find(What:="ListGrp2", LookAt:=xlWhole)
    Set rngTitle3 = rngValidation.Find(What:="ListGrp3", LookAt:=xlWhole)
    Set rngEnd = rngValidation.Find(What:="EndList", LookAt:=xlWhole)

    If Not rngTitle1 Is Nothing And Not rngTitle2 Is Nothing Then
        Set rngGrp1 = rngTitle1.Offset(1).Resize(rngTitle2.Row - rngTitle1.Row - 1, 1)
    End If

    If Not rngTitle2 Is Nothing And Not rngTitle3 Is Nothing Then
        Set rngGrp2 = rngTitle2.Offset(1).Resize(rngTitle3.Row - rngTitle2.Row - 1, 1)
    End If
    
    If Not rngTitle3 Is Nothing And Not rngEnd Is Nothing Then
        Set rngGrp3 = rngTitle3.Offset(1).Resize(rngEnd.Row - rngTitle3.Row - 1, 1)
    End If
    
    Cond = Not HasValidation(Target) And Not HasValidation(Target.Offset(1))
    strList = ""

    If Not Intersect(Target, rngGrp1) Is Nothing Then
        For Each cell In rngGrp1
            If Not cell = "" Then dGrp1.Add cell.Value, cell.Value
        Next
        'Create List
        For Each cell In rngListGrp1
            If Not dGrp1.Exists(cell.Value) Then
                strList = strList & "," & cell.Value
            End If
        Next
        If strList = "" Then
            MsgBox "All list item has been used"
            Exit Sub
        Else
            If Cond Then Target.Offset(1).EntireRow.Insert
        End If
        'Create Validation List
        With Target
            .Validation.Delete
            .Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=strList
        End With
    End If

    If Not Intersect(Target, rngGrp2) Is Nothing Then
        For Each cell In rngGrp2
            If Not cell = "" Then dGrp2.Add cell.Value, cell.Value
        Next
        'Create List
        For Each cell In rngListGrp2
            If Not dGrp2.Exists(cell.Value) Then
                strList = strList & "," & cell.Value
            End If
        Next
        If strList = "" Then
            MsgBox "All list item has been used"
            Exit Sub
        Else
            If Cond Then Target.Offset(1).EntireRow.Insert
        End If
        'Create Validation List
        With Target
            .Validation.Delete
            .Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=strList
        End With
    End If

    If Not Intersect(Target, rngGrp3) Is Nothing Then
        For Each cell In rngGrp3
            If Not cell = "" Then dGrp3.Add cell.Value, cell.Value
            If HasValidation(cell) Then ValCount = ValCount + 1
        Next
        'Create List
        For Each cell In rngListGrp3
            If Not dGrp3.Exists(cell.Value) Then
                strList = strList & "," & cell.Value
            End If
        Next
        If strList = "" Or ValCount = rngListGrp3.Count Then
            MsgBox "All list item has been used"
            Exit Sub
        Else
            If Cond Then Target.Offset(1).EntireRow.Insert
        End If
        'Create Validation List
        With Target
            .Validation.Delete
            .Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=strList
        End With
    End If
End If
Application.ScreenUpdating = True

End Sub

Function HasValidation(cell As Range) As Boolean

Dim valType As Long

HasValidation = True
On Error Resume Next
valType = cell.Validation.Type
If Err.Number <> 0 Then HasValidation = False
On Error GoTo 0

End Function
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,734
Members
449,094
Latest member
dsharae57

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