在 Excel VBA 中切换工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6310240/
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
Switching sheets in Excel VBA
提问by Andrew
I'm currently doing this Sheets("Sheet2").Range("A1").Value = 35
, but I want to a way to do just this Range("A1").Value = 35
, by switching the sheet on a separate line before. I tried Sheets("Sheet2").Select
, but that only switches the sheet it displays at the end to sheet 2.
我目前正在这样做Sheets("Sheet2").Range("A1").Value = 35
,但我想要一种方法来做到这一点Range("A1").Value = 35
,通过之前在单独的行上切换工作表。我试过Sheets("Sheet2").Select
,但这只会将最后显示的工作表切换到工作表 2。
回答by barrowc
You can use the With
keyword in VBA to achieve this. You can turn:
您可以使用With
VBA 中的关键字来实现此目的。你可以转:
Sheets("Sheet2").Range("A1").Value = 35
Sheets("Sheet2").Range("A1").Value = 35
into:
进入:
With Sheets("Sheet2")
.Range("A1").Value = 35
' do some other stuff
End With
Note the leading . before Range. This means: use the object from the With
statement
注意领先的 . 范围之前。这意味着:使用With
语句中的对象
Alternatively, you could assign the worksheet to a variable and just use that.
或者,您可以将工作表分配给一个变量并使用它。
Dim ws as Worksheet
Set ws = Sheets("Sheet2")
ws.Range("A1").Value = 35
回答by Dr. belisarius
Here is how you do that
这是你如何做到的
Sub a()
Sheets("Sheet2").Activate
Range("A1").Value = 35
End Sub