Excel VBA loop through multiple dependent drop down lists

Viewed 22

I have 4 dependent drop down lists with data validation, and I'm trying to loop through the entire list and make a copy of the workbook each time. I made a code using code from here: https://www.youtube.com/watch?v=OY5mqdcBdDk However, my third level drop down list isn't looping and I end up getting results with the wrong matches in the hierarchy. How can I fix this? My drop down lists are in cell C6, F6, C7, and F7. Any help would be appreciated!

Sub myFiles()

Dim wb As Workbook
Dim ws As Worksheet
Dim DVCell1 As Range
Dim DVCell2 As Range
Dim DVCell3 As Range
Dim DVCell4 As Range
Dim DVRange1 As Range
Dim DVRange2 As Range
Dim DVRange3 As Range
Dim DVRange4 As Range
Dim DVListItem1 As Range
Dim DVListItem2 As Range
Dim DVListItem3 As Range
Dim DVListItem4 As Range
Dim Path As String

Path = "C:\MyFiles\"

Set wb = ThisWorkbook
Set ws = wb.Worksheets("Template_view")
Set DVCell1 = ws.Range("C6")
Set DVRange1 = Evaluate(DVCell1.Validation.Formula1)
Set DVCell2 = ws.Range("F6")
Set DVRange2 = Evaluate(DVCell2.Validation.Formula1)
Set DVCell3 = ws.Range("C7")
Set DVRange3 = Evaluate(DVCell3.Validation.Formula1)
Set DVCell4 = ws.Range("F7")
Set DVRange4 = Evaluate(DVCell4.Validation.Formula1)


For Each DVListItem1 In DVRange1
    DVCell1 = DVListItem1
        For Each DVListItem2 In DVRange2
            DVCell2 = DVListItem2
                For Each DVListItem3 In DVRange3
                    DVCell3 = DVListItem3
                        For Each DVListItem4 In DVRange4
                            DVCell4 = DVListItem4
                            ws.Copy
                            Set nwb = ActiveWorkbook
                            Set nws = nwb.Worksheets("Template_view")
                                            
                            With nws
                            Cells.Copy
                            Cells.PasteSpecial (xlPasteFormulas)
                            End With
                                            
                            Application.DisplayAlerts = False
                            nwb.SaveAs Filename:=Path & DVListItem1 & DVListItem2 & DVListItem3 & DVListItem4 & ".xlsx", FileFormat:=xlWorkbookDefault
                            nwb.Close
                            Application.DisplayAlerts = True
                    
                        Next DVListItem4
                Next DVListItem3
        Next DVListItem2
Next DVListItem1

End Sub
0 Answers
Related