简体   繁体   English

Excel宏将一个工作簿中的列复制到另一个工作簿

[英]Excel macro copy columns from one workbook to another

I have a workbook: "Check for logging". 我有一本工作簿:“检查日志”。 From where I want to copy column "A" and paste it in workbook: "Unit test template". 我要从其中复制“ A”列并将其粘贴到工作簿中:“单元测试模板”。 In column "A". 在“ A”列中。

I have been trying to use following code: 我一直在尝试使用以下代码:

Private Sub CopyColumnsButton_Click()

Dim sourceColumn As Range, targetColumn As Range

Set sourceColumn = Workbooks("Check for logging.xlsm").Worksheets("Parameters").Columns("A")
Set targetColumn = Workbooks("Unit test template.xlsm").Worksheets("Sheet1").Columns("A")

sourceColumn.Copy Destination:=targetColumn

End Sub

Though every time I attempt to run the macro, I get a message saying: "Run-time error '9': Subscript out of range" and then this line is highlighted: 尽管每次尝试运行宏时,我都会收到一条消息:“运行时错误'9':下标超出范围”,然后此行突出显示:

Set targetColumn = Workbooks("Unit test template.xlsm").Worksheets("Sheet1").Columns("A")

Both workbooks are open when I attempt to run the macro. 当我尝试运行宏时,两个工作簿都是打开的。

I would be very thankfull if anyone could help 如果有人可以帮助我,我将非常感激

"Run-time error '9': Subscript out of range". “运行时错误'9':下标超出范围”。

It is mostly unable to find one of the two "Unit test template.xlsm" or "Sheet1" within the workbook. 通常无法在工作簿中找到两个“单元测试template.xlsm”或“ Sheet1”之一。

Workbook issue.. 工作簿问题
1. Check if the file name is "Unit test template.xls x " (not .xls m ) 1.检查文件名是否为“ Unit test template.xls x ”(不是.xls m
2. Check if the file "Unit test template.xlsm" is open in the same session as "Check for logging.xlsm" (If Ctrl+Tab doesn't take you to the next workbook, that means they are open in two different session of Excel Application) 2.检查是否在与“检查logging.xlsm”相同的会话中打开文件“ Unit test template.xlsm”(如果Ctrl + Tab不能将您带到下一个工作簿,则意味着它们以两种不同的方式打开申请表)
3. The Filename is slightly off. 3.文件名略微关闭。

Worksheet issue.. 工作表问题
1. Check if "Sheet1" was deleted. 1.检查“ Sheet1”是否已删除。 So it could actually be "Sheet2" etc... 因此实际上可能是“ Sheet2”等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM