Open new workbook => do some stuff => copy it => close "new" workbook without saving ?

chrisignm

Active Member
Joined
Apr 1, 2014
Messages
273
Hello. I want to do the following from an existing workbook:

1) Open new workbook
2) Do some stuff (for now its just inserting some random values)
3) Copy the sheet (I just need the values)
4) Close the new workbook WITHOUT saving
5) Insert values in the existing workbook

My attempt didn't work:

Code:
Sub NewWorkbookToOldWorkbook()
Workbooks.Add
Worksheets.Add(After:=Worksheets(1)).Name = "Update"
Range("A1").Value = "test"
Range("A2").Value = "test2"
Range("B5").Value = "test3"
Range("C2").Value = "C2"
Range("E5").Value = 1
Worksheets("Update").Copy
ActiveWorkbook.Close savechanges:=False ' Also tried ActiveWorkbook.Close False
'Worksheets("Rawdata").Cells.ClearContents
'Worksheets("Rawdata").Range("A1").PasteSpecial xlPasteValues
End Sub

The first issues I'm running into: Doesn't close the workbook! And If I do it without "False" it asks me to save it.

Please Help :)
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
1) Open new workbook
2) Do some stuff (for now its just inserting some random values)
3) Copy the sheet (I just need the values)
4) Close the new workbook WITHOUT saving
5) Insert values in the existing workbook

Hi - where do you want to copy the sheet to? Is it into the workbook where the code is?

It looks like your code is creating a copy as a new workbook and then closing it (as it is the active workbook) leaving the original newly created workbook open.
 
Upvote 0
Hey, I don't really understand what you mean. But maybe I try to explain it again.

What I would like to do is:

1) Open existing workbook, execute macro from there
Macro does the following:
2) Create/Open new workbook
3) Upload data from the internet inside there (Sheet "Upload") and do some changes on the data (I already got that working)
4) Copy all the values of "Upload"
5) Close the workbook that was opened in step 2 again. (don't save)
6) Delete the VALUES of sheet "RawData" in the existing workbook of step 1
7) Insert the values of step 4

Hope it is clear :)

My assumption is that the copied values of step 4 don't get lost with step 5. I hope that this is true.

Thanks a lot!
 
Last edited:
Upvote 0
This
Rich (BB code):
Worksheets("Update").Copy
creates a copy of the worksheet in a new workbook. It doesn't copy the values to the clipboard.
 
Last edited:
Upvote 0
OK, I think its Worksheets("Update").UsedRange.Copy then?

Edit: works :) Will post the code in a second
Thanks already for the hint!

Edit2:
Code:
Sub NewWorkbookToOldWorkbook()Workbooks.Add
Worksheets.Add(After:=Worksheets(1)).Name = "Update"
Range("A1").Value = "test"
Range("A2").Value = "test2"
Range("B5").Value = "test3"
Range("C2").Value = "C2"
Range("E5").Value = 1
Worksheets("Update").UsedRange.Copy
ActiveWorkbook.Close False
Worksheets("Rawdata").Cells.ClearContents
Worksheets("Rawdata").Range("A1").PasteSpecial xlPasteValues

End Sub


The very last code doesn't work => pasting the values
Runtime Error 1004 =>
pastespecial method of range class failed

It has the values copied to the clipboard! If I do manually Ctrl+V after macro failed, it pastes the values I want.
 
Last edited:
Upvote 0
This:
Rich (BB code):
Worksheets("Rawdata").Cells.ClearContents


Clears the clipboard, perhaps perform this step before the copy.
 
Upvote 0
This:
Rich (BB code):
Worksheets("Rawdata").Cells.ClearContents


Clears the clipboard, perhaps perform this step before the copy.

It has the values copied to the clipboard! If I do manually Ctrl+V after macro failed, it pastes the values I want.

However I tried your suggestion, still got the error on the last line.
 
Upvote 0
It appears that you can not use xlPasteValues after you have closed the source workbook.
 
Upvote 0
The problem seems to be with the range.

If I change the last line to that:
Worksheets("Rawdata").Range("A1:B2").PasteSpecial Paste:=xlPasteAll

It works. But it will only paste values from this range (so everything outside will not get paste)

How could I set the range of Worksheets("Update").UsedRange.Copy = Range in "Rawdata"?
 
Upvote 0
This works for me:
Rich (BB code):
Worksheets("Rawdata").Range("A1").PasteSpecial Paste:=xlPasteAll

xlPasteValues does not.
 
Upvote 0

Forum statistics

Threads
1,223,920
Messages
6,175,376
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top