Excel VBA根据工作表名称在单元格上创建下拉列表

时间:2019-04-15 02:00:51

标签: excel vba

我想基于工作表名称创建一个下拉列表。

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

所以***WorkSheetnames***在哪里,我想输入所有工作表名称

也许与

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

如何在不添加表格或其他东西的情况下执行此操作。

1 个答案:

答案 0 :(得分:2)

您几乎要达到目标

在添加代码之前

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

然后在验证make中

Formula1:=Shts