使用VBA将数据从一个工作簿传输到另一个工作簿

时间:2019-01-08 07:19:40

标签: excel vba

我想将数据从“ Finalinputsheet”传输到名为“ Mydata”的新工作簿,这是我尝试将要传输到新工作簿的列的代码

Sub transfer()
Dim MyData As Workbook
Dim DataWs As Worksheet
Dim myWs As Worksheet
Set myWs = ThisWorkbook.Sheets("FinalinputFile")
Set MyData = Workbooks.Open("D:\Desktop\My\MyData.xlsx")
Set DataWs = MyData.Sheets("Data")
myWs.Range("C3:C11000").Copy
DataWs.Range("E2").PasteSpecial xlPasteAll

Set myWs = ThisWorkbook.Sheets("FinalinputFile")
Set MyData = Workbooks.Open("D:\Desktop\My\MyData.xlsx")
Set DataWs = MyData.Sheets("Data")
myWs.Range("E3:E11000").Copy
DataWs.Range("F2").PasteSpecial xlPasteAll




MyData.Save

End Sub

我已经尝试过了,但是并没有给出不确定我哪里出错的输出

This is the sheet image

1 个答案:

答案 0 :(得分:0)

您没有得到输出,因为您的所有代码都在做的主要是选择一个范围并获取列数。下面的代码应该可以解决问题。只需更改文件的路径即可。

Sub transfer()
Dim MyData As Workbook
Dim DataWs As Worksheet
dim myWs as Worksheet
set myWs = thisworkbook.sheets("FinalinputFile")
Set MyData = Workbooks.Open("D:\Desktop\Data.xlsx")
Set DataWs = MyData.Sheets("Sheet1")
Myws.Range("C3:C440").Copy
DataWs.Range("E2").PasteSpecial xlPasteAll

MyData.Save

End Sub