0

I am trying to run a macro using an existing workbook that 1) opens a different workbook I'll call other 2) copies values from other workbook 3) pastes values from other into existing workbook and 4) closes other workbook. Below is the code I have so far. Is there a way to better define or set y? My code works except I am not able to close other and I think if I had y better defined I could just use something like y.close. Thanks!

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
2
  • you need to get an workbook object than access the worksheets in it. Also give your variable names something meaningful so you can distinguish what they are. Commented Feb 22, 2016 at 21:22
  • Do you have an example you could provide? Commented Feb 22, 2016 at 21:26

1 Answer 1

1

Untested

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
Sign up to request clarification or add additional context in comments.

1 Comment

Worked great. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.