Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 从另一个工作簿复制工作表,工作表名称可以根据输入而更改_Excel_Vba - Fatal编程技术网

Excel 从另一个工作簿复制工作表,工作表名称可以根据输入而更改

Excel 从另一个工作簿复制工作表,工作表名称可以根据输入而更改,excel,vba,Excel,Vba,对于工作,我们有一个包含许多不同工作表的excel工作簿,我的角色是每次从其中一个工作表中获取信息,并将其放入另一个工作簿中进行工作。真的很简单,我目前正在学习VBA,并试图从自动化这个过程开始 这就是我目前所拥有的。它从按下“新数据的宏测试空白表.xlsm”工作簿上的按钮开始 Workbooks.Open "C:\Macro Test\2019-2020 Reflecto Record.xlsm" Workbooks("2019-2020 Reflecto Record.xlsm").Wo

对于工作,我们有一个包含许多不同工作表的excel工作簿,我的角色是每次从其中一个工作表中获取信息,并将其放入另一个工作簿中进行工作。真的很简单,我目前正在学习VBA,并试图从自动化这个过程开始

这就是我目前所拥有的。它从按下
“新数据的宏测试空白表.xlsm”
工作簿上的按钮开始

Workbooks.Open "C:\Macro Test\2019-2020 Reflecto Record.xlsm"

 Workbooks("2019-2020 Reflecto Record.xlsm").Worksheets(1).Range("A1:BA10000").Copy _
    Workbooks("Macro Test Blank Sheet For new Data.xlsm").Worksheets("Data").Range("A1")

ActiveWorkbook.Close
完成后,数据已被复制,并且
“2019-2020反射记录.xlsm”
工作簿已关闭


目前,它正在使用
.Worksheet(1)
来使用工作簿中的第一页进行复制。在启动宏的按钮旁边的单元格中输入名称时,是否可以按名称选择工作表?

如果在名为“Sheet1”的工作表的单元格A1中指定名称(只需将名称更改为正在工作的工作表即可)

使用此代码:

Dim sourceSheetName as String

sourceSheetName = Thisworkbook.Worksheets("Sheet1").Range("A1").Value

Workbooks.Open "C:\Macro Test\2019-2020 Reflecto Record.xlsm"

Workbooks("2019-2020 Reflecto Record.xlsm").Worksheets(sourceSheetName).Range("A1:BA10000").Copy _
        Workbooks("Macro Test Blank Sheet For new Data.xlsm").Worksheets("Data").Range("A1")

ActiveWorkbook.Close

我还建议,如果您在名为“Sheet1”的工作表的单元格A1中指定名称(只需将名称更改为您正在工作的工作表),您可以研究自动化此类过程

使用此代码:

Dim sourceSheetName as String

sourceSheetName = Thisworkbook.Worksheets("Sheet1").Range("A1").Value

Workbooks.Open "C:\Macro Test\2019-2020 Reflecto Record.xlsm"

Workbooks("2019-2020 Reflecto Record.xlsm").Worksheets(sourceSheetName).Range("A1:BA10000").Copy _
        Workbooks("Macro Test Blank Sheet For new Data.xlsm").Worksheets("Data").Range("A1")

ActiveWorkbook.Close

我还建议您研究自动化此类流程,这应该可以让您开始,两种建议的生成名称的方法:

Sub sheetopener()
Dim MySh As String 'Dim MySh as a variable to hold the name of your sheet.

MySh = InputBox("Which sheet?", "Sheet picker") 'Set the name of your sheet with a popup box.
'Mysh = Thisworkbook.Sheet1.Range("A2").value 'Alternative set the name of your sheet in reference to a cell.

Workbooks("Name here").Sheets(MySh).Activate 'Activate the sheet with the just generated name.

End Sub

这将为您提供两种生成名称的建议方法:

Sub sheetopener()
Dim MySh As String 'Dim MySh as a variable to hold the name of your sheet.

MySh = InputBox("Which sheet?", "Sheet picker") 'Set the name of your sheet with a popup box.
'Mysh = Thisworkbook.Sheet1.Range("A2").value 'Alternative set the name of your sheet in reference to a cell.

Workbooks("Name here").Sheets(MySh).Activate 'Activate the sheet with the just generated name.

End Sub

您也可以使用输入框或从下拉列表中选择,而不是使用按钮旁边的单元格。您也可以使用输入框或从下拉列表中选择,而不是使用按钮旁边的单元格。