VBA Excelmacros将一列复制到另一个工作簿
我有2个工作簿,并试图find一种方法将wb1的列复制到wb2中。 我知道我可以复制/粘贴,但想法是做一些东西,所以我的老板可以点击macros,一切都填充。
我一直在尝试我遇到的另一个问题的代码:
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
当我运行这个,我得到一个“下标超出范围”错误。
源列包含一个依赖于其他列的公式,而目标列是空的,但是即使当我在小数字的虚拟工作簿上运行这个时,我也得到了相同的错误。
有什么超级基础我在这里失踪?
编辑:刚刚意识到可能缺less的那段代码。 在那里添加一个“:”! 更改为destination:=Workbooks("....
它应该工作正常。
额外的细节:当使用函数参数时,你必须添加“:”以便为计算机指定你没有计算相等性,但要做一个参数赋值。
(老,华丽的答案)正如我认为这是你想要做的; 这个脚本可能会做你想做的。 风格将不被保留。
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
我没有时间来testing这个呢。 将尽快做。