Home » excel » Excel VBA Create Dropdown List on Cell based on Worksheetnames

Excel VBA Create Dropdown List on Cell based on Worksheetnames

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to create a dropdownlist based on worksheetnames.

Worksheets("MainSheet").Range("A1").Select

With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:= ***WorkSheetnames***
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With

So where ***WorkSheetnames*** is, I wanted to put all worksheetnames

Maybe something with

for i = 1 to application.sheets.count 
'Store data into array?
Names = sheets(i).name
next

How can I do this without adding a sheet or something..

How to&Answers:

you are almost near goal

Before your code add

Dim Shts As String
    For i = 1 To Sheets.Count
        Shts = Shts & ThisWorkbook.Sheets(i).Name & ","
    Next
    Shts = Left(Shts, Len(Shts) - 1)

then within validation make

Formula1:=Shts