简体   繁体   English

Excel VBA-将2个工作簿中的多个列复制到1个工作簿中

[英]Excel VBA - copy multiple columns from 2 workbooks into 1 workbook

I have two workbooks that have data in multiple columns that I want copied and put into a new workbook that contains this macro. 我有两个工作簿,这些工作簿具有要复制的多个列中的数据,并放入包含此宏的新工作簿中。 Basically I want to be able to open the current workbook, "z", run the macro, and have the data move/copy over from the other two existing workbooks. 基本上,我希望能够打开当前工作簿“ z”,运行宏,并使数据从其他两个现有工作簿移入/复制。

I have been searching around here and looking at different ideas from different posts, and think I have pretty much figured this out but am running into a "Run-time error 1004" when I run this code. 我一直在这里搜索,并从不同的帖子中寻找不同的想法,并认为我已经很清楚了,但是在运行此代码时遇到了“运行时错误1004”。 The error happens at the first y.Range line, and I have yet to figure out why. 错误发生在第一行y.Range行,我还没有弄清楚为什么。

I have successfully been able to pull the data from "x" workbook but not "y". 我已经成功地能够从“ x”工作簿中提取数据,但不能从“ y”中提取数据。

I am new to VBA so any help would be greatly appreciated. 我是VBA的新手,所以我们将不胜感激。

Thanks, 谢谢,

Sub SellPrice()

Dim x As Worksheet, y As Worksheet, z As Worksheet, LastRow&

Workbooks.Open ("C:\Users\tsmith\Desktop\SellPrice\PRODUCT.XLS")
Workbooks.Open ("C:\Users\tsmith\Desktop\SellPrice\GrossProfit.xls")

Set x = Workbooks("PRODUCT.XLS").Worksheets("ProductFile")
Set y = Workbooks("GrossProfit.xls").Worksheets("Sellprice")
Set z = Workbooks("SellPriceMacro.xlsm").Worksheets("Sheet1")

LastRow = x.Cells.SpecialCells(xlCellTypeLastCell).Row

x.Range("B4:B" & LastRow).Copy z.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
x.Range("C4:C" & LastRow).Copy z.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
x.Range("K4:K" & LastRow).Copy z.Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)

LastRow = y.Cells.SpecialCells(xlCellTypeLastCell).Row

y.Range("B2:B" & LastRow).Copy z.Cells(Rows.Count, "E").End(x1Up).Offset(1, 0)
y.Range("C2:C" & LastRow).Copy z.Cells(Rows.Count, "F").End(x1Up).Offset(1, 0)
y.Range("D2:D" & LastRow).Copy z.Cells(Rows.Count, "G").End(x1Up).Offset(1, 0)
y.Range("H2:H" & LastRow).Copy z.Cells(Rows.Count, "H").End(x1Up).Offset(1, 0)

Application.CutCopyMode = False

End Sub

See how you put the worksheet before you used Range() ? 看看在使用Range()之前如何放置工作表? ( y.Range(...) ) - you need to do that any time you use Cells() , Rows.Count , and Columns.Count (and other ranges). y.Range(...) -你需要做的,任何时候你用Cells() Rows.CountColumns.Count (和其他范围)。 Otherwise, it's going to look to whatever the ActiveSheet is, and use that. 否则,它将查看ActiveSheet是什么,然后使用它。 When you mix inactive sheets with active sheets, you'll get an error. 当您将非活动工作表与活动工作表混合在一起时,会出现错误。

Sub SellPrice()

Dim x As Worksheet, y As Worksheet, z As Worksheet, LastRow&

Workbooks.Open ("C:\Users\tsmith\Desktop\SellPrice\PRODUCT.XLS")
Workbooks.Open ("C:\Users\tsmith\Desktop\SellPrice\GrossProfit.xls")

Set x = Workbooks("PRODUCT.XLS").Worksheets("ProductFile")
Set y = Workbooks("GrossProfit.xls").Worksheets("Sellprice")
Set z = Workbooks("SellPriceMacro.xlsm").Worksheets("Sheet1")

LastRow = x.Cells.SpecialCells(xlCellTypeLastCell).Row

x.Range("B4:B" & LastRow).Copy z.Cells(z.Rows.Count, "A").End(xlUp).Offset(1, 0)
x.Range("C4:C" & LastRow).Copy z.Cells(z.Rows.Count, "B").End(xlUp).Offset(1, 0)
x.Range("K4:K" & LastRow).Copy z.Cells(z.Rows.Count, "C").End(xlUp).Offset(1, 0)

LastRow = y.Cells.SpecialCells(xlCellTypeLastCell).Row

y.Range("B2:B" & LastRow).Copy z.Cells(z.Rows.Count, "E").End(xlUp).Offset(1, 0)
y.Range("C2:C" & LastRow).Copy z.Cells(z.Rows.Count, "F").End(xlUp).Offset(1, 0)
y.Range("D2:D" & LastRow).Copy z.Cells(z.Rows.Count, "G").End(xlUp).Offset(1, 0)
y.Range("H2:H" & LastRow).Copy z.Cells(z.Rows.Count, "H").End(xlUp).Offset(1, 0)

Application.CutCopyMode = False

End Sub

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

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