VBA for Copy and Paste between 2 workbooks

VBAuser41

New Member
Joined
Dec 2, 2013
Messages
16
Hello. I am trying to create code that copy and pastes information from sheet 1 (the whole sheet) of an xlsx file, we'll call it "Workbook 1", located on a users desktop (could be any user, but will always be on their desktop), into sheet 2 of a different xlsx file, called "Workbook 2". The pasted data would need to be Transposed as well. No other restrictions. How would I do this?

Thank you.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">Assuming you're running the code out of Workbook2.

Code:
</code>Sub ocp()

Dim thiswb As Workbook
Dim otherwb As Workbook

Set thiswb = ActiveWorkbook

userlocation = CreateObject("WScript.Shell").specialfolders("Desktop")


Workbooks.Open (userlocation & "\Book1.xlsx")
Set otherwb = ActiveWorkbook
otherwb.Sheets("Sheet1").Copy After:=thiswb.Sheets(Sheets.Count)
otherwb.Close


End Sub


<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">



</code>
 
Last edited:
Upvote 0
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">Assuming you're running the code out of Workbook2.

Code:
</code>Sub ocp()

Dim thiswb As Workbook
Dim otherwb As Workbook

Set thiswb = ActiveWorkbook

userlocation = CreateObject("WScript.Shell").specialfolders("Desktop")


Workbooks.Open (userlocation & "\Book1.xlsx")
Set otherwb = ActiveWorkbook
otherwb.Sheets("Sheet1").Copy After:=thiswb.Sheets(Sheets.Count)
otherwb.Close


End Sub


<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">



</code>
Thanks for the reply. This almost gets me there. This adds a new tab to book 2 and it does not transpose the pasted data. sheet 1 in book 2 is reading sheet 2, so adding a new tab is not an option. Can you adjust so it takes those things into consideration? Thanks!
 
Upvote 0
I'm a bit lost here, you want to copy the sheet and paste over an existing one?.

Try adjusting this to work this into it :)

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">thiswb.Worksheets("Sheet1").Range("A1:A5").Copy
otherwb.Worksheets
("Sheet1").Range("A1").PasteSpecial Transpose:=True</code>
 
Upvote 0
Sorry, I realized you can't transpose by copying the entire sheet. So I Need to copy from A1 in book 1 down and over as far as there are contents in the cells. Similar to when you do ctrl+shift down arrow/right arrow.
 
Upvote 0
The macro recorder is handy for working things like this out, in this case it's.

Code:
Range("A1").Select    
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
 
Last edited:
Upvote 0
Yep, got that far. I finally got it working using what you created and some tweaking. Thank you for the help!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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