Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I copy a sheet in Excel VBA from one workbook to another replacing it if it already exists.

Posted on 2014-09-29
8
Medium Priority
?
503 Views
Last Modified: 2014-09-30
Hi All,

I want to copy an Excel worksheet from workbook A to workbook B, checking to see if a sheet of that name already exists on workbook B. If it does, then I want to delete it, replacing it with the sheet from workbook A.

My question is: can you do this in one VBA statement ?

thanks
Toco
0
Comment
Question by:Tocogroup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40350317
No - you have to do it with separate functions, because Microsoft does not want you to accidentally overwrite data and not be able to undo it.
0
 
LVL 34

Assisted Solution

by:Norie
Norie earned 1000 total points
ID: 40350318
Toco

Short answer - no.

First you would need code to check if there was a worksheet in workbook B with the same name worksheet you want to copy from workbook A.

Then you would need code to delete it if required

Finally you do the copying.

The latter 2 are one-liners but the first part wouldn't be.
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 40350406
Can I suggest you confirm your understanding of the terminology?

One VBA Statement - do you mean one instruction / line within a routine or do you mean one set of instructions / lines making up a routine?

The way I try and understand a VBA routine is usually to imagine the thought process if you were doing it manually and then flow chart it. For your example, the steps I suspect would be:

Activate Source Workbook
Copy Data
Activate Destination Workbook
Q1 - Does required sheet Exist?
If Q1 = Yes - Q2 Does it already contain data?
If Q2 = Yes - Clear data and paste
If Q2 = No - Paste
If Q1 = No - Create Sheet, paste data.

Thanks
Rob H
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:Norie
ID: 40350476
Toco

Is it an entire sheet or is it data from a sheet you want to copy?
0
 

Author Comment

by:Tocogroup
ID: 40351539
It's an entire sheet
0
 
LVL 33

Accepted Solution

by:
Rob Henson earned 1000 total points
ID: 40351686
I take it though if the sheet already exists it will be just data.

If so the check is simpler as you only have two possible actions, paste data or copy sheet.
0
 

Author Closing Comment

by:Tocogroup
ID: 40352227
Thanks for your joint help.

Yes, the steps are as follows:

1. Check if sheet exists in workbook.
2. If true, delete sheet.
3. Copy sheet
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 40352450
Rather than Delete sheet and then insert sheet, why not overwrite existing data with copied data.

In source files the following vba statement will copy the whole sheet:

Cells.Copy

Thanks
Rob
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question