I have three dropdown validation lists and I am trying to write some code that iterates through all of the "Management Methods" permutations that are available.
I can figure out the first step of iterating through one list (for example Iterate through an Excel dropdown/validation list and others) but I can't work out how to go through three of them.
Ideally, I would like this to be written in a way that works even if you add more options.
To achieve this I figure you want a way of counting how many options there are in each list and then iterating through from 0-n.
Any help would be much appreciated.
Sub LoopThroughList()
Dim Dropdown1, Dropdown2, Dropdown3 As String
Dim Range1, Range2, Range3 As Range
Dim option1, option2, option3 As Range
' *** SET DROPDOWN LOCATIONS HERE ***
' ***********************************
Dropdown1 = "C6"
Dropdown2 = "D6"
Dropdown3 = "E6"
' ***********************************
' ***********************************
Set Range1 = Evaluate(Range(Dropdown1).Validation.Formula1)
Set Range2 = Evaluate(Range(Dropdown2).Validation.Formula1)
Set Range3 = Evaluate(Range(Dropdown3).Validation.Formula1)
For Each option1 In Range1
For Each option2 In Range2
For Each option3 In Range3
Worksheets("Sheet1").Range("C6:E6").Copy
With Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteValues
End With
Next option3
Next option2
Next option1
End Sub
At the moment I get this:
This is iterating through the crop type dropdown but returning the same for pest and management methods. The code I used to create the dropdown list is as below:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("C6")) Is Nothing Then
Range("D6:E6").ClearContents
End If
End Sub