Copy Sheet from a workbook to current workbook

larinda4

Board Regular
Joined
Nov 15, 2021
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Good morning,

I have this code below and I'm trying to tweak it to work for my situation. I'm trying to copy a sheet from a closed workbook into my current active workbook that's open. The location of the sheet/workbook I need to copy from always remains the same and the workbook I'm copying it into is always the active workbook but the name of the workbook is different. Any help is appreciated.

VBA Code:
Sub CopySheet()

Workbooks.Open Filename:="*Enter File Location Here*"
Workbooks("Revised").Sheets("Copy").Copy Before:=ACTIVE WORKBOOK.Sheets("Glossary")
Workbooks("Revised.xls").Close savechanges:=False

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Identify the active workbook before opening the the other workbook.
Here is a sample, you may have to edit it

VBA Code:
dim wb as workbook,bk as workbook
set wb=activeworkbook

set bk=Workbooks.Open(Filename:="filename")
with bk
.Sheets("Copy").Copy Before:=wb.Sheets("Glossary")
bk.close false
end with
 
Upvote 0
Solution
Identify the active workbook before opening the the other workbook.
Here is a sample, you may have to edit it

VBA Code:
dim wb as workbook,bk as workbook
set wb=activeworkbook

set bk=Workbooks.Open(Filename:="filename")
with bk
.Sheets("Copy").Copy Before:=wb.Sheets("Glossary")
bk.close false
end with
It worked perfect! Thank you so much!
 
Upvote 0
Identify the active workbook before opening the the other workbook.
Here is a sample, you may have to edit it

VBA Code:
dim wb as workbook,bk as workbook
set wb=activeworkbook

set bk=Workbooks.Open(Filename:="filename")
with bk
.Sheets("Copy").Copy Before:=wb.Sheets("Glossary")
bk.close false
end with
Hey dave,

This was working great for me last week and now when I try to run it I get a "Run-time error '1004': Excel cannot insert the sheets into the destination workbook because it contains fewer rows and columns than the source workbook."

Do you know what I did to make the run time error come up?
 
Upvote 0
Let's see the code you are using, showing the workbook names
Hey Dave! Thanks for getting back to me. I figured it out. When I tried running it yesterday it was on a spreadsheet saved as .xls and I was using a newer extension like .xlsm.

It worked after I saved a new copy of the file. Thank you for all your help!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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