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

x
?
Solved

Copy worksheet to a new workbook

Posted on 2014-03-09
5
Medium Priority
?
373 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
[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
  • 2
5 Comments
 
LVL 31

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 31

Accepted Solution

by:
gowflow earned 1000 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 34

Assisted Solution

by:Norie
Norie earned 1000 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 34

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

704 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