vba 在一行中引用多个工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17745459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:19:50  来源:igfitidea点击:

Reference multiple sheets in one line

excelvba

提问by user1305569

I was wondering if there is a way to reference multiple sheets in a single line in Excel VBA. I know you are able to reference multiple ranges, columns, rows via:

我想知道是否有办法在 Excel VBA 中的一行中引用多个工作表。我知道您可以通过以下方式引用多个范围、列、行:

Range("G1:G5, G6:G10, H5:H10") etc...

I want to be able to do the same thing with sheets I tried this:

我希望能够对我试过的床单做同样的事情:

Sheets("Sheet1_Name, Sheet2_Name, Sheet3_Name").Range(...)

But that didn't work so I was wondering if there was a special way to do this aside from putting the sheet names into variables then referencing the variables.

但这不起作用,所以我想知道除了将工作表名称放入变量然后引用变量之外,是否还有一种特殊的方法可以做到这一点。

回答by dee

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

    Dim target As Range
    Dim sheetObject As Worksheet

    ' change value of range 'a1' on each sheet from sheetsArray
    For Each sheetObject In sheetsArray
        Set target = sheetObject.Range("A1")
        target.Value = "Test"
    Next sheetObject
End Sub