Open another workbook and copy to sheet

mrkambo

New Member
Joined
May 7, 2017
Messages
23
i have 2 files
Main.xlsm
Database.xlsx

What i want to do its on Main workbook open, open Database.xlsx and copy it to a sheet in Main called "exceptions" and then close Database.xlsx

Im currently opening Database with:

Code:
Workbooks.Open (ActiveWorkbook.Path & "\Database.xlsx")
As its in the same directory

Any guidance would be appreciated
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
What part of Database.xlsx do you want to copy - a worksheet, part of a worksheet, all worksheets, ....? What's the name or names of the sheets you want to copy?
 
Upvote 0
Its only 1 worksheet, its called sheet1 for now
Untested so may need some tweaking:
Code:
Sub mrkambo()
'run this from the Main workbook
Dim Main As Workbook, DB As Workbook
Set Main = ThisWorkbook
Application.ScreenUpdating = False
Workbooks.Open Filename:=Main.Path & "\Database.xlsx"
Set DB = ActiveWorkbook
DB.Sheets(1).Move after:=Main.Sheets(Sheets.Count)
ActiveSheet.Name = "exceptions"
DB.Close savechanges:=False
'rest of code, if any
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Excellent this works

However i get a

Run-time error '-2147221080 (900401a8)

Method 'Close of object' _Workbook' failed

But it does close the database workbook
 
Upvote 0
Excellent this works

However i get a

Run-time error '-2147221080 (900401a8)

Method 'Close of object' _Workbook' failed


But it does close the database workbook
You are welcome - thanks for the reply.

As for the error, I had forgotten you have only one sheet in Database.xlsx.
Remove this line and the error should be gone.
Code:
DB.Close savechanges:=False
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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