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

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

最佳答案

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

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




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

关于arrays - 工作表变量数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21513026/

10-10 05:50