Forum Discussion

dward9273gmailcom's avatar
dward9273gmailcom
Copper Contributor
Aug 16, 2024

File Saving macro from three cells

I am in search of a macro to save all Excel workbooks in the same folder with a filename that is a combination of cell A1 and B3 and b4. Windows 10 and Office 365. Thank you for sharing your expertise and time.

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    dward9273gmailcom 

    You can create a macro in Excel that will save all open workbooks in the same folder with a filename that is a combination of the values in cells A1, B3, and B4. Here’s how you can do it:

    1. Open Excel and press ALT + F11 to open the Visual Basic for Applications (VBA) editor.
    2. Insert a new module: Click on Insert > Module.
    3. Copy and paste the following VBA code into the module:

    Vba code is untested backup your file first.

    Sub SaveAllWorkbooks()
        Dim wb As Workbook
        Dim folderPath As String
        Dim fileName As String
        Dim savePath As String
        
        ' Get the folder path of the active workbook
        folderPath = ThisWorkbook.Path
        
        ' Get the filename from cells A1, B3, and B4
        fileName = ThisWorkbook.Sheets(1).Range("A1").Value & "_" & _
                   ThisWorkbook.Sheets(1).Range("B3").Value & "_" & _
                   ThisWorkbook.Sheets(1).Range("B4").Value & ".xlsx"
        
        ' Loop through all open workbooks
        For Each wb In Application.Workbooks
            ' Construct the full save path
            savePath = folderPath & "\" & fileName
            
            ' Save the workbook with the new filename
            wb.SaveAs Filename:=savePath, FileFormat:=xlOpenXMLWorkbook
        Next wb
        
        MsgBox "All workbooks saved with the filename: " & fileName
    End Sub

     

    Explanation of the Code:

    • ThisWorkbook.Path: Gets the folder path of the workbook where the macro is running.
    • fileName: Concatenates the values from cells A1, B3, and B4 in the first sheet and adds the .xlsx extension.
    • For Each wb In Application.Workbooks: Loops through each open workbook in Excel.
    • wb.SaveAs: Saves each workbook using the constructed fileName.

    Steps to Use:

    1. Customize the cell references (A1, B3, B4) if needed, depending on where your data is located.
    2. Run the macro by pressing F5 or by assigning it to a button in your workbook.

    This macro will save each open workbook in the same folder as the active workbook with a filename that combines the values from cells A1, B3, and B4. The text, steps and code were created with the help of AI.

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

    Was the answer useful? Mark as best response and Like it!

    This will help all forum participants.

Resources