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
431 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
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 33

Assisted Solution

by:Norie
Norie earned 250 total points
Comment Utility
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 31

Expert Comment

by:Rob Henson
Comment Utility
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
 
LVL 33

Expert Comment

by:Norie
Comment Utility
Toco

Is it an entire sheet or is it data from a sheet you want to copy?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Tocogroup
Comment Utility
It's an entire sheet
0
 
LVL 31

Accepted Solution

by:
Rob Henson earned 250 total points
Comment Utility
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
Comment Utility
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 31

Expert Comment

by:Rob Henson
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now