vba 在vba中使用变量选择Excel工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28046898/
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
Using a variable to select an Excel sheet in vba
提问by Stan
I have the following Excel VBA code.
我有以下 Excel VBA 代码。
I already copied some data from another Excel document. I need to paste these date in a second document that contains an Excel sheet for every week.
我已经从另一个 Excel 文档中复制了一些数据。我需要将这些日期粘贴到包含每周 Excel 工作表的第二个文档中。
The Excel sheets are called "week 01", "week 02", "..." , "week 52".
Excel 工作表称为“第 01 周”、“第 02 周”、“...”、“第 52 周”。
Now I need to select the sheet with the correct number to paste my copied data there.
现在我需要选择具有正确编号的工作表以将我复制的数据粘贴到那里。
Sub Macro1()
Dim number As String
Dim year As String
number = Application.InputBox(Prompt:="insert week number (01-52))", Type:=2)
year = Application.InputBox(Prompt:="insert year (YYYY)", Type:=2)
Workbooks.Open Filename:= _
"F:\documents\" & year & "\example " & number & ".xlsm"
Sheets("Week" number).Select
Range("M4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
I know that the following code isn't correct. How can I select the correct excel sheet, using the variable "number"?
我知道以下代码不正确。如何使用变量“数字”选择正确的 Excel 工作表?
Sheets("Week" number).Select
回答by Dubison
about the below code, did you miss to put "&" in between, or you just miss typed it?
关于下面的代码,您是否错过了将“&”放在中间的机会,或者您只是错过了输入?
Sheets("Week" number).Select
should be
应该
Sheets("Week " & number).Select 'there is a space after week??
and please pay attention to the spaces between " ". They can mass up your code. like the one below. there is a space after "\example ":
并请注意“ ”之间的空格。他们可以堆积你的代码。像下面那个。“\example”后有一个空格:
"F:\documents\" & year & "\example " & number & ".xlsm"