工作表变量数组

时间:2014-02-02 16:30:40

标签: arrays excel vba excel-vba

根据文档,我可以定义下面的工作表数组

Dim sheetsArray As Sheets
Set sheetsArray = ActiveWorkbook.Sheets(Array("Sheet1", "Sheet2"))

但是如果我的工作表是由下面的指针变量动态创建和指向的话会发生什么?我如何制作它们的数组,以便我可以循环它们来进行相同的活动?

Dim tempsheet1 As Worksheet
Dim tempsheet2 As Worksheet

Set tempsheet1 = Sheets.Add
Set tempsheet2 = Sheets.Add

Dim sheetsArray As Sheets
Set sheetsArray = ActiveWorkbook.Sheets(Array(tempsheet1, tempsheet2)) ' Doesn't work
For Each msheets In sheetsArray
    With msheets
        DO SOMETHING
    End With
Next msheets

1 个答案:

答案 0 :(得分:3)

由于您应在Array中指定工作表名称,请更改

Set sheetsArray = ActiveWorkbook.Sheets(Array(tempsheet1, tempsheet2))

Set sheetsArray = ActiveWorkbook.Sheets(Array(tempsheet1.Name, tempsheet2.Name))