Solved

Copy worksheet to a new workbook

Posted on 2014-03-09
5
366 Views
Last Modified: 2014-03-09
I'm using the following code to copy a sheet to a new workbook but it's only copying the values, not the formulas. How can I modify this code to copy the formulas too?

Sub CopyToNew()
Dim ws As Worksheet
    Sheets(Array("SO")).Copy
    For Each ws In ActiveWorkbook.Worksheets
        ws.UsedRange.Value = ws.UsedRange.Value
    Next ws
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs "\\BCAR1\BCAR-Net\IT\Excel Spreadsheets\Daily SO Report\Daily SO Report.xlsx"
    Application.DisplayAlerts = True
End Sub

Open in new window

0
Comment
Question by:Lawrence Salvucci
  • 2
  • 2
5 Comments
 
LVL 29

Expert Comment

by:gowflow
ID: 39916004
this code is not copying the value !!!

What do you want to copy ?

It is copying
Sheets(Array("SO")).Copy

but it is not pasting it.

what do you have sheets that you need to copy ?
gowflow
0
 
LVL 29

Accepted Solution

by:
gowflow earned 250 total points
ID: 39916026
ok in your code replace

this line
ws.UsedRange.Value = ws.UsedRange.Value


by this line
ws.Paste


so that the code becomes

Sub CopyToNew()
Dim ws As Worksheet
    Sheets("sheet1").Copy
    For Each ws In ActiveWorkbook.Worksheets
        ws.Paste
    Next ws
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs "\\BCAR1\BCAR-Net\IT\Excel Spreadsheets\Daily SO Report\Daily SO Report.xlsx"
    Application.DisplayAlerts = True
End Sub

Open in new window

0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 250 total points
ID: 39916042
Remove this, it's the part of the code that's replacing formulas with values.
For Each ws In ActiveWorkbook.Worksheets
        ws.UsedRange.Value = ws.UsedRange.Value
    Next ws

Open in new window

0
 
LVL 1

Author Closing Comment

by:Lawrence Salvucci
ID: 39916107
Thank you very much for your quick responses!
0
 
LVL 33

Expert Comment

by:Norie
ID: 39916131
goflow

What's being pasted here?
ws.Paste

Open in new window

When I try the code it's the last thing that was copied to the clipboard, and nothing to do with the worksheet that's been copied.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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;…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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 …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

680 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