使用文件路径名从一个工作簿复制到另一个工作簿

时间:2016-02-22 21:18:02

标签: excel excel-vba vba

我正在尝试使用现有工作簿运行宏1)打开另一个工作簿我将其称为其他 2)从其他<复制值< / em>工作簿3)将其他中的值粘贴到现有工作簿中,然后关闭其他工作簿。下面是我到目前为止的代码。有没有办法更好地定义或设置y?我的代码工作,除了我无法关闭其他,我想如果我有更好的定义我可以使用像y.close这样的东西。谢谢!

Dim x As Worksheet
Dim y As Worksheet

Set x = ActiveWorkbook.Sheets("File44")

Workbooks.Open (ThisWorkbook.Path & "\File44.xlsm")
Workbooks("File44.xlsm").Activate

Set y = ActiveWorkbook.Sheets("File44 - Detail")
LastRow = y.Range("A" & Rows.Count).End(xlUp).Row

y.Range("A15:CQ" & LastRow).Copy
x.Range("A2").PasteSpecial xlPasteValues

ActiveWorkbook.Close savechanges:=False

1 个答案:

答案 0 :(得分:1)

<强>未测试

Sub Chris2015()

Dim other As Workbook
Dim fromWS As Worksheet
Dim toWS As Worksheet
Dim lastRow As Long

Set toWS = ThisWorkbook.Sheets("File44") 'pasting to this worksheet
Set other = Workbooks.Open(ThisWorkbook.path & "\File44.xlsm")
Set fromWS = other.Sheets("File44 - Detail")

lastRow = fromWS.Range("A" & fromWS.Rows.Count).End(xlUp).Row

toWS.Range("A2:CQ" & lastRow - 13).Value2 = fromWS.Range("A15:CQ" & lastRow).Value2 'hope I did math right XD

other.Close savechanges:=False

End Sub