VBA Excel宏将一列复制到另一工作簿
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21033956/
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
VBA Excel Macro Copying One Column into Another Workbook
提问by Aubrey
I have 2 workbooks and am trying to find a way to copy a column from wb1 into wb2. I am aware I could just copy/paste, but the idea is to make something so my boss can click the macro and everything populates.
我有 2 个工作簿,正在尝试找到一种方法将 wb1 中的列复制到 wb2 中。我知道我可以只复制/粘贴,但我的想法是制作一些东西,这样我的老板就可以点击宏,然后一切都会填充。
I have been trying a code I came across in another question:
我一直在尝试在另一个问题中遇到的代码:
Sub Import()
Dim sourceColumn As Range
Dim destColumn As Range
Set sourceColumn = Workbooks("C:\Documents and Settings\********\My Documents\*********.xlsm").Worksheets(2).Columns("BL")
Set destColumn = Workbooks("C:\Documents and Settings\********\My Documents\*********.xlsm").Worksheets(2).Columns("A")
sourceColumn.Copy Destination = destColumn
End Sub
When I run this, I get a "Subscript Out Of Range" Error.
当我运行它时,出现“下标超出范围”错误。
The source column contains a formula relying on other columns and the destination column is empty, but even when I ran this on dummy workbooks with small digits I got the same error.
源列包含一个依赖于其他列的公式,目标列是空的,但即使我在小数字的虚拟工作簿上运行它,我也遇到了同样的错误。
Is there something super basic I am missing here?
我在这里缺少一些超级基本的东西吗?
采纳答案by Bernard Saucier
EDIT :Just realized what's probably missing from that piece of code you have. Add a ":" in there! Change to destination:=Workbooks("....
and it should work fine.
编辑:刚刚意识到您拥有的那段代码中可能缺少什么。在那里添加一个“:”!更改为destination:=Workbooks("....
它应该可以正常工作。
Extra details :When working with function parameters, you have to add the ":" in order to specify to the computer you're not evaluating an equality, but doing a parameter assignment.
额外细节:使用函数参数时,您必须添加“:”,以便向计算机指定您不是在评估相等性,而是在进行参数分配。
(Old, flashy answer) As I assume this is what you're trying to do; this script will probably do what you want. The style will NOT be preserved however.
(旧的,华而不实的答案)我认为这就是你想要做的;这个脚本可能会做你想做的。但是,不会保留样式。
Sub yourSub()
Application.ScreenUpdating = False 'Disables "Screen flashing" between 2 workbooks
Dim colA As Integer, colB As Integer
Dim rowA As Integer, rowB As Integer
Dim wbA As Workbook, wbB As Workbook
Set wbA = ThisWorkbook
Set wbB = Workbooks.Open("C:/YourFilePath/YourFile.xls")
colA = 1 'Replace "1" with the number of the column FROM which you're copying
colB = 1 'Replace "1" with the number of the column TO which you're copying
rowA = 1 'Replace "1" with the number of the starting row of the column FROM which you're copying
rowB = 1 'Replace "1" with the number of the row of the column TO which you're copying
wbA.Activate
lastA = Cells(Rows.Count, colA).End(xlUp).Row 'This finds the last row of the data of the column FROM which you're copying
For x = rowA To lastA 'Loops through all the rows of A
wbA.Activate
yourData = Cells(x, colA)
wbB.Activate
Cells(rowB, colB) = yourData
rowB = rowB + 1 'Increments the current line of destination workbook
Next x 'Skips to next row
Application.ScreenUpdating = True 'Re-enables Screen Updating
End Sub
I didn't have time to test this yet. Will do as soon as possible.
我还没有时间测试这个。会尽快做。