Excel VBA to export and save sheet as a new workbook without formulas

Status
Not open for further replies.

emadhamrawi

New Member
Joined
Nov 11, 2020
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I need a VBA code that helps me export an excel sheet and save it as an excel workbook without formulas.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Welcome to the MrExcel Message Board!

Try the following macro in a standard module. It is only three lines actually, but looks more with comments. Follow the comments to understand how it works.

VBA Code:
Sub saveSheetWithoutFormulas()
    ' Copy the active sheet
    ' This automatically creates a new workbook
    ' with the sheet copied in it
    ActiveSheet.Copy
    
    ' Active sheet is now the copied sheet in the new workbook
    ' Following will change formulas to values
    ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
    
    ' Open a file save dialog to save the new workbook
    ' If you know where to save with a predefined file name
    ' then you can use ActiveWorkbook.SaveAs "C:\FolderPath\FileName.xlsx"
    Application.GetSaveAsFilename

End Sub

If you want to make it work with a certain worksheet which is not supposed to be the active sheet when you run the code, then you need to change the first line, ActiveSheet.Copy, with the following by using the actual sheet name instead of "Sheet1".

VBA Code:
Worksheets("Sheet1").Copy
 
Upvote 0
Welcome to the MrExcel Message Board!

Try the following macro in a standard module. It is only three lines actually, but looks more with comments. Follow the comments to understand how it works.

VBA Code:
Sub saveSheetWithoutFormulas()
    ' Copy the active sheet
    ' This automatically creates a new workbook
    ' with the sheet copied in it
    ActiveSheet.Copy
   
    ' Active sheet is now the copied sheet in the new workbook
    ' Following will change formulas to values
    ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
   
    ' Open a file save dialog to save the new workbook
    ' If you know where to save with a predefined file name
    ' then you can use ActiveWorkbook.SaveAs "C:\FolderPath\FileName.xlsx"
    Application.GetSaveAsFilename

End Sub

If you want to make it work with a certain worksheet which is not supposed to be the active sheet when you run the code, then you need to change the first line, ActiveSheet.Copy, with the following by using the actual sheet name instead of "Sheet1".

VBA Code:
Worksheets("Sheet1").Copy
Thank you so much for your quick response. That was perfect. I just need one more thing. I used the path for the saveas, but I want the file name to be the value of cell H1. How can I do that?
 
Upvote 0
I used the path for the saveas, but I want the file name to be the value of cell H1. How can I do that?
Does it mean that you don't want to open the Save As dialog, but automatically save the file by using the file name (with full path) written in cell H1?
 
Upvote 0
Both option could be useful for the future readers. So, I'll answer for both.

If you still want to see the Save As dialog but with a default file name which is provided in cell H1, then change the last code line with the following:
VBA Code:
Application.GetSaveAsFilename Range("H1")

If the path and file name is predefined, and stored in cell H1, then use the following instead:
VBA Code:
ActiveWorkbook.SaveAs Range("H1")
 
Upvote 0
Both option could be useful for the future readers. So, I'll answer for both.

If you still want to see the Save As dialog but with a default file name which is provided in cell H1, then change the last code line with the following:
VBA Code:
Application.GetSaveAsFilename Range("H1")

If the path and file name is predefined, and stored in cell H1, then use the following instead:
VBA Code:
ActiveWorkbook.SaveAs Range("H1")
Path and filename are predefined but only the file name is stored in H1.
 
Upvote 0
Path and filename are predefined but only the file name is stored in H1.
Where is the path then if it is not in H1?

Did you notice my previous message contains ActiveWorkbook.SaveAs - which I also mentioned in the original code?
 
Upvote 0
Where is the path then if it is not in H1?

Did you notice my previous message contains ActiveWorkbook.SaveAs - which I also mentioned in the original code?
Hello Sir! If I want to save in current excel file path
 
Upvote 0
Hello Sir! If I want to save in current excel file path
Answered your question in its own thread:
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,224,920
Messages
6,181,769
Members
453,065
Latest member
jfrsanders

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