Solved

Copy worksheet to a new workbook

Posted on 2014-03-09
5
364 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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;…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
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 the scrolling table in Microsoft Excel using the INDEX function.

776 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