Copying A Sheet from One Workbook to Another with VBA

UncleBajubjubs

Board Regular
Joined
Jul 11, 2017
Messages
111
Office Version
  1. 2010
Hello, I have a workbook, "Purchasing Workbook", which has a worksheet called "Vendor Data" on which a list of parts is manually entered in, then sent to whomever to purchase the parts. The parts are determined by a different workbook "Data Workbook", and the information is then manually entered into the "Vendor Data" worksheet in "Purchasing Workbook".


To make it so the information doesn't have to be manually entered after it's been calculated, I copied the "Vendor Data" worksheet into "Data Workbook", and then had all the cells auto-fill with the correct parts information. My goal is that after "Vendor Data" is completely filled in on the "Data Workbook", the user presses a button in the "Data Workbook" to copy it over to "Purchasing Workbook" either directly on the sheet, or it makes a new sheet and the old one is deleted.


As both "Purchasing Workbook" and "Data Workbook" are templates that will be saved as new documents with the name of the project they are for, I had to write the code to open a dialog box allow the user to pick which file to open and copy the data to.


I wrote code to do this, but didn't realize "Purchasing Workbook" is a 1997-2003 document, and "Data Workbook" is the newest version. Trying to copy the sheet gives the error "Excel cannot insert the sheets into the destination workbook, because it contains fewer rows and columns than the source workbook. To move or copy the data to the destination workbook, you can select the data, and then use Copy and Paste commands to insert it into the sheets of another workbook.".


So now I'm stuck again. Is there another way to do this? The data on the worksheet is entirely contained through A1-AJ191, so I'm thinking there's a way to just copy that range, but don't know exactly how I'd code it.

Thanks.
 
Thanks again, I ran that and got the "File Error" message, so I added a msgbox line between the other lines to see which was giving it the error.

<code>
Workbooks(FileNm.Name).Sheets("Vendor Data").Range("A1").PasteSpecial xlPasteValues
</code>
Is the line that is giving it trouble, I'm looking at it but I don't see anything wrong.
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
U changed the sheet names. So...
Code:
ThisWorkbook.Sheets("Vendor Data Sheets").Range("A1:AJ191").Copy
Workbooks(FileNm.Name).Sheets("VENDOR COMP DATA").Range("A1").PasteSpecial xlPasteValues
Dave
 
Upvote 0
I had corrected the sheet names, I was just copying from what you had written rather than my VBA. It's definitely copying the data correctly, as after it gives the file error message, the proper range is still selected.
 
Upvote 0
I tested the code before I posted and it worked. I have re-tested the code and it works. Trial opening a new workbook and pasting in the code. Change all the sheet names in the code to "Sheet1". Run the sub and select a wb that has a sheet1 available and transfer the data. It works. It seems that your sheet naming is still funky. You can trial commenting out this line of code to find out where the error actually is. Dave
Code:
'On Error GoTo below
 
Upvote 0
Alrighty thank you, so I did as you suggested and I can indeed copy the page to a new workbook, and then I commented out the error line and found that
<code>


Workbooks(FileNm.Name).Sheets("VENDOR COMP DATA").Range("A1").PasteSpecial xlPasteValues

</code>

which gave me the error message : "This operation requires the merged cells to be identically sized."

I'm thinking I'll have to unmerge all of cells before copying, and then remerge them after they've pasted, unless there is an easier way.
 
Upvote 0
Merged cells were never mentioned and I have no idea what effect they have on copying and pasting. Have to search Google to find out. Dave
 
Upvote 0

Forum statistics

Threads
1,223,706
Messages
6,173,998
Members
452,542
Latest member
Bricklin

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