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

x
?
Solved

Copy Worksheets to another workbook

Posted on 2013-05-11
2
Medium Priority
?
419 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
[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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

664 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