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
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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
 
Upvote 0
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: 8
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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)
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,214,594
Messages
6,120,436
Members
448,964
Latest member
Danni317

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
Back
Top