Solved

Copy Worksheets to another workbook

Posted on 2013-05-11
2
368 Views
Last Modified: 2013-05-11
I need to create vba code that will copy two specific worksheets from my active workbook to another open workbook. But I need it to paste values and not copy the formulas & links. Can anyone help?
0
Comment
Question by:Lawrence Salvucci
2 Comments
 
LVL 4

Assisted Solution

by:Jorgen
Jorgen earned 250 total points
Comment Utility
Hi isalvucci

If you tried searching for "copy Worksheets to another workbook, you can find more than 5.000 related questions and save yourself some time. I selected no 4 as a solution that was nice VBA code thanks to matthewspatrick.
 
When you have copied your sheet, create a macro and mark everything in the sheet and copy followed by paste as value.

You can do this nicer, but if you need help on a saturday evening, that will have to be a quick and dirty solution working for you.

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
Set objWorkbook1= objExcel.Workbooks.Open("SourceFile.xlsx")
Set objWorkbook2= objExcel.Workbooks.Open("TargetFile.xlsx")
objWorkbook1.Sheets(Array("Sheet1","Sheet2","Sheet3")).Copy _
    objWorkbook2.Sheets(1)
objWorkbook1.save
objWorkbook2.save
objWorkbook1.close
objWorkbook2.close
objExcel.Quit
set objExcel=nothing
0
 
LVL 80

Accepted Solution

by:
byundt earned 250 total points
Comment Utility
I've modifed Jorgen's code so it copies the worksheets and replaces formulas with the values from the original workbook. I also got rid of the need to instantiate a new instance of Excel and to hardcode the names of source and target workbooks.

As revised, the macro assumes that the source workbook is active and that the target workbook is the only other workbook that is open.
Sub CopySheetValues()
Dim wb As Workbook, wbTarg As Workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each wb In Application.Workbooks
    If wb.Name <> ActiveWorkbook.Name Then
        Set wbTarg = wb
        Exit For
    End If
Next
For Each ws In ActiveWorkbook.Sheets(Array("Sheet1", "Sheet2"))
    ws.Copy After:=wbTarg.Sheets(wbTarg.Sheets.Count)
    ActiveSheet.UsedRange.Value = ws.UsedRange.Value
Next
End Sub

Open in new window

Brad
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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

17 Experts available now in Live!

Get 1:1 Help Now