Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Copy Worksheets to another workbook

Posted on 2013-05-11
2
Medium Priority
?
423 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 1000 total points
ID: 39158580
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 81

Accepted Solution

by:
byundt earned 1000 total points
ID: 39158625
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

Technology Partners: 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!

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

927 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