Solved

Copy Worksheets to another workbook

Posted on 2013-05-11
2
380 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
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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Struggling to Lock and grey out cells using VBA. Help please! 8 23
Problem to With line 4 39
Excel Score Formula 5 47
Excel copy picture into Outlook email 7 44
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 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…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

911 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

16 Experts available now in Live!

Get 1:1 Help Now