Copy sheet from file to file

O

Otto Moehrbach

Excel XP & Win XP
wbFile2 and wbFDB are variables and are set to 2 open workbooks. Both have
a sheet called "Names".
I want to delete the "Names" sheet in wbFDB and copy the "Names" sheet from
wbFile2 to wbFDB.
The sheet does get deleted in wbFDB. Good!
But the last line, to copy the sheet, does not work.
Sub CopyNamesSht()
'wbFile2 is the active workbook
Application.DisplayAlerts = False
wbFDB.Sheets("Names").Delete
Application.DisplayAlerts = True
Sheets("Names").Copy After:=wbFDB.Sheets("COJDatabase")
End Sub
What am I doing wrong in the last line? Thanks for your time. Otto
 
C

Chip Pearson

Your code worked fine for me. What problems are you having? I would suggest
that you prefix any reference to a worksheet with the workbook reference. It
makes things easier since you don't have to worry about what workbook
happens to be active at the time.

Sheets("Names").Copy After:=wbFDB.Sheets("COJDatabase")
is better to write as
wbFile2.Sheets("Names").Copy After:=wbFDB.Sheets("COJDatabase")


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
O

Otto Moehrbach

Chip
Thanks for that. I don't know if this is a problem but both files have
the exact same code except for the particular code that is the subject of
this thread. And there is a lot of code in these files. Could that upset
Excel?
I could delete ALL the code from the wbFile2 workbook if that would
help, but I would have to do that via VBA. Thanks again. Otto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top