Saving an excel file with multiple sheets as values using a cell reference as the file name

Peter Davison

Active Member
Joined
Jun 4, 2020
Messages
437
Office Version
  1. 365
Platform
  1. Windows
Using VBA code, can you advise how to save a workbook with multiple sheets removing all formulas and save as values retaining all formatting, but using a name entered in "Cell I2" as the file name?

Thanks for your help
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
VBA Code:
Sub OverwriteFormulaWithValuesAndSave()
    Dim sh As Worksheet
    Dim FileName as string
    For Each sh In ActiveWorkbook.Worksheets
        With sh.UsedRange
            .Value = .Value
        End With
    Next sh
    FileName=Sheets("XXX").Range("I2").value
    ActiveWorkbook.SaveAs filename:= FileName & ".xlsm", FileFormat:=xlNormal
End Sub
 
Upvote 0
Thank you I really appreciate your time.
As some of my sheets have a chart that is linked to other workbooks, can the code also make the charts just a picture with no source? Or any other way?
Cheers
 
Upvote 0
Should works like this:

VBA Code:
Option Explicit
Sub OverwriteFormulaWithValuesAndSave()
    Dim sh As Worksheet
    Dim FileName As String
    Dim MyChrt As Object
    Application.ScreenUpdating = False
    For Each sh In ActiveWorkbook.Worksheets
        With sh
            For Each MyChrt In .ChartObjects
                MyChrt.CopyPicture
                .Paste
                With .Shapes(.Shapes.Count)
                    .Top = MyChrt.Top
                    .Left = MyChrt.Left
                    .Name = MyChrt.Name & "_pasted_as_pic"
                End With
                MyChrt.Delete
            Next MyChrt
        End With
        With sh.UsedRange
            .Value = .Value
        End With
    Next sh
    FileName = Sheets("XXX").Range("I2").Value
    ActiveWorkbook.SaveAs FileName:=FileName & ".xlsm", FileFormat:=xlNormal
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you I will give this a try and let you know how it goes.
What would I be entering here - "_pasted_as_pic"
 
Upvote 0
As some of my sheets have a chart that is linked to other workbooks, can the code also make the charts just a picture with no source? Or any other way?
Instead of copying and pasting the charts as a picture, you could break the links to the other workbooks:
VBA Code:
Sub OverwriteFormulaWithValuesAndSave2()
    Dim sh As Worksheet
    Dim FileName As String
    For Each sh In ActiveWorkbook.Worksheets
        With sh.UsedRange
            .Value = .Value
        End With
    Next sh
    
    Dim ExternalLinks As Variant, i As Long
    With ActiveWorkbook
        ExternalLinks = .LinkSources(Type:=xlLinkTypeExcelLinks)
        For i = 1 To UBound(ExternalLinks)
            .BreakLink Name:=ExternalLinks(i), Type:=xlLinkTypeExcelLinks
        Next
    End With
    
    FileName = Sheets("XXX").Range("I2").Value
    ActiveWorkbook.SaveAs FileName:=FileName & ".xlsm", FileFormat:=xlNormal
End Sub
 
Upvote 0
Hi John W
I tried the formula and all worked okay until the save file.
It says that it is trying to save in a 97-03 version (so I exceed rows)

Is it something to do with the line -
ActiveWorkbook.SaveAs FileName:=FileName & ".xlsm", FileFormat:=xlNormal

Should the xlNormal be different?
Could the file also save without the macro.

That would be great if you can help.
Cheers
 
Upvote 0
Hi John, Kokosek,

I have adjusted the formula to what I think is correct and added some message boxes.
I'm not sure I've got the first message box correct.
If they click cancel I want the routine to end, but I think it just goes back to the top and starts again
I think the word "End" should be something else, but just not sure what?

This is the code

Sub OverwriteFormulaWithValuesAndSave2()

Dim sh As Worksheet
Dim FileName As String

'Message Box to continue or abort
Dim answer1 As Integer
answer1 = MsgBox("Please Note: This process will remove the user selection options click OK to continue or Cancel", vbOKCancel)
If answer1 = vbOK Then
MsgBox "OK"
Else
MsgBox "Cancel"
End
End If

'Overwrites all formulas as values
For Each sh In ActiveWorkbook.Worksheets
With sh.UsedRange
.Value = .Value
End With
Next sh

'Breaks links with charts
Dim ExternalLinks As Variant, i As Long
With ActiveWorkbook
ExternalLinks = .LinkSources(Type:=xlLinkTypeExcelLinks)
For i = 1 To UBound(ExternalLinks)
.BreakLink Name:=ExternalLinks(i), Type:=xlLinkTypeExcelLinks
Next
End With

'Message Box before saving to name specified in Cell C1
Dim answer2 As Integer
answer2 = MsgBox("The File is now ready to Save - Click ok to continue", vbOKOnly)

'Saves file to name specified in Cell C1
Application.DisplayAlerts = False
FileName = Sheets("Reports Index").Range("C1").Value
ActiveWorkbook.SaveAs FileName:=FileName & "xlsx", FileFormat:=51
Application.DisplayAlerts = True
End Sub

If you can help me that would be great.

Regards
 
Upvote 0
Sub aaa()
Dim answer1 As Integer
answer1 = MsgBox("Please Note: This process will remove the user selection options click YES to continue or NO to stop procedure", vbYesNo)
If answer1 = vbYes Then
MsgBox "OK go then.."
Else
MsgBox "You choose NO. Procedure has stop."
Exit Sub
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,840
Members
449,096
Latest member
Erald

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