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

问题描述 投票:1回答:1

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

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

如何在不添加表格或其他内容的情况下执行此操作..

excel vba
1个回答
2
投票

你几乎接近目标

在您的代码添加之前

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

然后在验证中

Formula1:=Shts
© www.soinside.com 2019 - 2024. All rights reserved.