我发现对于我的示例来说,两个数组的串联/追加是一个过于繁琐的过程。但是如何在单个 block 中使用 For Each 迭代两个工作表数组(如何缩短以下代码)?
arr1 = Array("Sheet2", "Sheet3")
arr2 = Array("Sheet5", "Sheet6")
For Each sh In Sheets(arr1)
sh.Visible = True
Next sh
For Each sh In Sheets(arr2)
sh.Visible = True
Next sh
最佳答案
您始终可以将小型数组组合为 super 数组。例如:
Sub Klai()
arr1 = Array("Sheet2", "Sheet3")
arr2 = Array("Sheet5", "Sheet6")
arr3 = Array(arr1, arr2)
For Each a In arr3
For Each b In a
MsgBox Sheets(b).Name
Sheets(b).Visible = True
Next b
Next a
End Sub
关于vba - Excel VBA : Consolidate the iterating group in "For Each" block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38420965/