【问题标题】:Making a Collection of Sheet Names制作工作表名称的集合
【发布时间】:2020-11-11 10:00:00
【问题描述】:

我正在尝试生成工作簿中所有工作表的集合。

Sub collMaker()

Dim coll As Collection, ws As Worksheet, x As String

For i = 2 To Application.Sheets.count
    x = Application.Sheets(i).name
    coll.Add Item:=x, Key:=x
Next i

End Sub

我在coll.add 行收到运行时错误“91”。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    你需要在创建你的收藏时使用New,否则它只会被定义为Nothing,你不能添加一些东西。

    Dim 你的所有变量也是一个好主意。

    Sub collMaker()
    
    Dim coll As New Collection, ws As Worksheet, x As String
    Dim i As Integer
    
    For i = 2 To Application.Sheets.Count
         x = Application.Sheets(i).Name
         coll.Add Item:=x, Key:=x
    Next i
    
    End Sub
    

    What does the keyword 'New' do in VBA?

    【讨论】:

    • @Alastair:我们在这里,当有人回答我们的问题(以一种非常有帮助的方式)时,勾选代码左侧的复选框,以使其接受答案。如果其他人会搜索类似的问题,他会知道答案解决了您的问题并且代码有效(现在)。
    • @FaneDuru,对不起,我在回答问题后尝试这样做,但现在点击还为时过早。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多