copy and save excel worksheet without formulas

primep

New Member
Joined
May 29, 2020
Messages
10
Office Version
  1. 2013
Platform
  1. Windows
I want to copy and save a worksheet "Template", below VBA is working good, but it is copying also the formulas, I want only cell values of above work to be copied and saved.

Thanks

Private Sub CommandButton1_Click() ' Save Excel Copy

Dim FileName As String

Dim FilePath As String

Dim NewBook As Workbook

FilePath = "D:\"

FileName = Sheets("Template").Range("g14") & ".xls"

If Dir(FilePath & "\" & FileName) <> "" Then

MsgBox "File " & FilePath & "\" & FileName & " already exists", vbInformation

Exit Sub

Else

Set NewBook = Workbooks.Add

ThisWorkbook.Sheets("Template").Copy before:=NewBook.Sheets(1)

Application.DisplayAlerts = False

NewBook.SaveAs FileName:=FilePath & FileName

NewBook.Activate

On Error Resume Next

ActiveSheet.OLEObjects.Visible = True

ActiveSheet.OLEObjects.Delete

On Error GoTo 0

NewBook.Save

NewBook.Close

End If

End Sub
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,412
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
How about
VBA Code:
Set NewBook = Workbooks.Add

ThisWorkbook.Sheets("Template").Copy before:=NewBook.Sheets(1)
With NewBook.Sheets(1).UsedRange
   .Value = .Value
End With
Application.DisplayAlerts = False
 

primep

New Member
Joined
May 29, 2020
Messages
10
Office Version
  1. 2013
Platform
  1. Windows
Hi Fluff...
Thanks for your prompt guidance,
I added the code as per your reply, but VBA code is working same as before, its copying also the formulas,
please see the attached image
 

Attachments

  • vba.png
    vba.png
    24.9 KB · Views: 4

primep

New Member
Joined
May 29, 2020
Messages
10
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

ok below is the original code, please edit as required...
VBA Code:
Private Sub CommandButton1_Click() ' Save Excel Copy
    Dim FileName As String
    Dim FilePath As String
    Dim NewBook As Workbook
    Dim ws As Worksheet
    FilePath = "D:\"
    FileName = Sheets("Template").Range("g14") & ".xls"
    If Dir(FilePath & "\" & FileName) <> "" Then
        MsgBox "File " & FilePath & "\" & FileName & " already exists", vbInformation
        Exit Sub
    Else
    Set NewBook = Workbooks.Add
    ws.UsedRange.Value = ws.UsedRange.Value
    ThisWorkbook.Sheets("Template").Copy before:=NewBook.Sheets(1)
    With NewBook.Sheets(1).UsedRange
   .Value = .Value
    End With
    Application.DisplayAlerts = False
    NewBook.SaveAs FileName:=FilePath & FileName
    NewBook.Activate
    On Error Resume Next
    ActiveSheet.OLEObjects.Visible = True
    ActiveSheet.OLEObjects.Delete
    On Error GoTo 0
    NewBook.Save
    NewBook.Close
    End If
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,412
Office Version
  1. 365
Platform
  1. Windows
I would expect this line
VBA Code:
    ws.UsedRange.Value = ws.UsedRange.Value
to give you an error as you have not assigned anything to ws
 

primep

New Member
Joined
May 29, 2020
Messages
10
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

Hi Fluff...
But the original code is working fine without any error,
could you please suggest a fresh new code , as per the original code results (with only cell values to be saved in a new workbook, without showing formulas)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,412
Office Version
  1. 365
Platform
  1. Windows
The code I suggested will do exactly what you asked for. If the code you posted is your exact code, then I see no way that it can work.
 

primep

New Member
Joined
May 29, 2020
Messages
10
Office Version
  1. 2013
Platform
  1. Windows
hi fluff,
sorry you were right, please see below original code...
VBA Code:
Private Sub CommandButton1_Click() ' Save Excel Copy
    Dim FileName As String
    Dim FilePath As String
    Dim NewBook As Workbook
    FilePath = "D:\"
    FileName = Sheets("Template").Range("g14") & ".xls"
    If Dir(FilePath & "\" & FileName) <> "" Then
        MsgBox "File " & FilePath & "\" & FileName & " already exists", vbInformation
        Exit Sub
    Else
    Set NewBook = Workbooks.Add
    ThisWorkbook.Sheets("Template").Copy before:=NewBook.Sheets(1)
    With NewBook.Sheets(1).UsedRange
   .Value = .Value
    End With
    Application.DisplayAlerts = False
    NewBook.SaveAs FileName:=FilePath & FileName
    NewBook.Activate
    On Error Resume Next
    ActiveSheet.OLEObjects.Visible = True
    ActiveSheet.OLEObjects.Delete
    On Error GoTo 0
    NewBook.Save
    NewBook.Close
    End If
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,412
Office Version
  1. 365
Platform
  1. Windows
That should work & convert the formulae to values.
Step through the code using F8 until you get to this line
VBA Code:
    ThisWorkbook.Sheets("Template").Copy before:=NewBook.Sheets(1)
Then in the VB editor have a look at the project for the new workbook, how many sheets does it contain?
 

Watch MrExcel Video

Forum statistics

Threads
1,127,328
Messages
5,624,036
Members
416,007
Latest member
csf

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top