Using a loop to create multiple dependent data validation lists in vba

lostitagain

New Member
Joined
Jul 5, 2012
Messages
21
Using Excel 2010 in Windows 7 32-bit

I am attempting to use a vba macro to generate a number of drop down lists depending on how many entries the user requires.

Each entry consists of the entry number, a data validation list, and a dependent data validation list.
and should look like:
Number of Entries:</SPAN>
3</SPAN>
Entry #</SPAN>
List</SPAN>
Selection</SPAN>
1</SPAN>
List1</SPAN>
Choice A</SPAN>
2</SPAN>
List 3</SPAN>
Choice X</SPAN>
3</SPAN>
List 2</SPAN>
Choice 3</SPAN>

<TBODY>
</TBODY>

Where each item in the List column is a Data Validation drop down to choose which List to select from and each item in the Selection Column is a dependent Data Validation drop down that is dependent on the choice on the same row in the list column.

Generating multiple data validation lists works fine. The code generates a Runtime Error '1004': application-defined or object defined error at the line in red. I'm pretty sure it has to do with the syntax of the Indirect statement using a variable for the row number but I am unable to correct it.

Any help would be appreciated.

Code:
Sub FillDataEntry()
'Declare Variables
Dim i, EntryNum As Integer
'Store the number of entries a user wishes to make
EntryNum = Range("B1").Value
'Initialize i as 2 since it is the row that the entries start on
i = 2
'Loops to create a number of lists equal to the user entry
While i < 2 + EntryNum
   'Enters the Entry number
   Range("D" & i) = i - 1
   'Main List for the E column
   With Range("E" & i).Validation
       .Delete
       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
       xlBetween, Formula1:="=List"
       .IgnoreBlank = True
       .InCellDropdown = True
       .InputTitle = ""
       .ErrorTitle = ""
       .InputMessage = ""
       .ErrorMessage = ""
       .ShowInput = True
       .ShowError = True
   End With
   'Makes a list in the F column that is dependent on the list in the E column
   With Range("F" & i).Validation
      .Delete
[COLOR=#ff0000]    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
      xlBetween, Formula1:="=Indirect(E" & i & ")"
[/COLOR]    .IgnoreBlank = True
      .InCellDropdown = True
      .InputTitle = ""
      .ErrorTitle = ""
      .InputMessage = ""
      .ErrorMessage = ""
      .ShowInput = True
      .ShowError = True
   End With
   i = i + 1
Wend
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,216,117
Messages
6,128,937
Members
449,480
Latest member
yesitisasport

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