VBA copy formulas to values and change filename

vilamall

New Member
Joined
Jun 24, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi guys!

I want to export a spreadsheet to a folder located on the desktop.

I have two problems:
I want to convert formulas to cell values in the exported sheet (formulas that result in blank cells have to remain blank in the exported sheet).
I also want to change the name of the exported sheet to: Spreadsheet name & "_" & File name & ".xlsx".

Thank you.

VBA Code:
Sub ExportWorksheets()

    Dim worksheet_list As Variant, worksheet_name As Variant
    Dim new_workbook As Workbook
    Dim saved_folder As String

    worksheet_list = Array("Export")
    
    '// makes sure you close the path with a back slash \
    saved_folder = Environ("userprofile") & "\Desktop\Export folder\"
    
    
    For Each worksheet_name In worksheet_list
    
        On Error Resume Next
        ' Opens a new Excel wokrobook
        Set new_workbook = Workbooks.Add
        
        ThisWorkbook.Worksheets(worksheet_name).Copy new_workbook.Worksheets(1)
        
        new_workbook.SaveAs saved_folder & worksheet_name & ".xlsx", 51
        new_workbook.Close False
        
    Next worksheet_name
    
    MsgBox "Export complete.", vbInformation
    
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Not sure I understand how you want to rename the file.

VBA Code:
Sub ExportWorksheets()

    Dim worksheet_list As Variant, worksheet_name As Variant
    Dim new_workbook As Workbook
    Dim saved_folder As String
    Dim WS As Worksheet

    worksheet_list = Array("Export")

    '// makes sure you close the path with a back slash \
    saved_folder = Environ("userprofile") & "\Desktop\Export folder\"

    For Each worksheet_name In worksheet_list
        Set WS = Nothing
        Set new_workbook = Nothing

        ' Opens a new Excel wokrobook
        On Error Resume Next
        Set new_workbook = Workbooks.Add
        Set WS = ThisWorkbook.Worksheets(worksheet_name)
        On Error GoTo 0

        If Not new_workbook Is Nothing And Not WS Is Nothing Then
            WS.Copy new_workbook.Worksheets(1)
            WS.UsedRange.Copy
            new_workbook.Worksheets(worksheet_name).Range("A1").PasteSpecial (xlPasteValues)

            Application.DisplayAlerts = False
            new_workbook.SaveAs saved_folder & worksheet_name & ".xlsx", 51
            Application.DisplayAlerts = True
            new_workbook.Close False
        End If
    Next worksheet_name

    MsgBox "Export complete.", vbInformation
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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