Excel VBA - fill in-cell dropdown

I'm using this to loop 5 times to fill an in-cell dropdown.

Sub Add_Drop_Down_Menu_Selection()

    For i = 1 To 5
    
        With Selection.Validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, _
        Formula1:="MyData"
        .IgnoreBlank = True
        .InCellDropdown = True
        End With

    Next i

End Sub

Open in new window


But I get Runtime error 1004.
It works fine for the 1st run through the loop. I want it to ADD more items until the loop ends.

Thanks!
hindersalivaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
Use it this way...

Sub Add_Drop_Down_Menu_Selection()
    Dim str As String


    For i = 1 To 5
        If str = "" Then
            str = "MYData"
        Else
            str = str & "," & "MYData"
        End If

    Next i
    With Selection.Validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, _
             Formula1:=str
        .IgnoreBlank = True
        .InCellDropdown = True
    End With

End Sub

Open in new window


Saurabh...
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
When you create a data validation list manually by typing the items in the List Source, you simply type the items separated by comma.

The same approach will work in your scenario. So you will need to create a string of comma separated items and then add that list as a data validation list source.

Also you need to add .Delete before adding a data validation to a cell because if the cell already has a data validation, it will produce an error.

And even if you run your code after adding .Delete, it will only add one item "My Data" in the list.

Try the following code which will add 5 items to the list source like MyData 1, MyData2..... MyData5.
Sub Add_Drop_Down_Menu_Selection()
Dim i As Long, vList As String

For i = 1 To 5
    vList = vList & "," & "MyData " & i
Next i

vList = Right(vList, Len(vList) - 1)

With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, _
    Formula1:="My Data"
    .IgnoreBlank = True
    .InCellDropdown = True
End With
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.