File Save Macro

ward9273

New Member
Joined
Aug 12, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Can you assist?
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. Windows 10 and Office 365. Thank you for sharing your expertise and time.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Not totally clear what you're asking for, but see if this macro works for you. It renames all the workbooks (*.xlsx) in the selected folder using the values in cells A1 and B3 of the first worksheet in each workbook.

VBA Code:
Public Sub Rename_Workbooks()

    Dim folderPath As String
    Dim wbFileName As String, wbNewFileName As String
    Dim targetWb As Workbook
                
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select Folder"
        If .Show Then
            folderPath = .SelectedItems(1) & "\"
        Else
            Exit Sub
        End If
    End With
        
    Application.ScreenUpdating = False
        
    wbFileName = Dir(folderPath & "*.xlsx")
    While wbFileName <> vbNullString
    
        Set targetWb = Workbooks.Open(folderPath & wbFileName, ReadOnly:=True)
        wbNewFileName = targetWb.Worksheets(1).Range("A1").Value & targetWb.Worksheets(1).Range("B3").Value & Mid(wbFileName, InStrRev(wbFileName, "."))
        targetWb.Close SaveChanges:=False
        
        Name folderPath & wbFileName As folderPath & wbNewFileName
        
        DoEvents
        wbFileName = Dir
    Wend
    
    Application.ScreenUpdating = True
    
    MsgBox "Finished"
    
End Sub
 
Upvote 0
Solution
John, Thank you for your time and expertise! I am pretty sure I can take it from here. :)
 
Upvote 0
John, Thank you for your time and expertise! I am pretty sure I can take it from here. :)
John, It failed to run? Can you assist. I am trying to keep it simple. As you already know, I am a newbie.
I put the date and time in "a1" and the partial file name in B3. When I am done with the WB i want to combine a1 and b3 into a time and date stamped filename. Ultimately, I want to store all of my excel wb in the same folder.
 
Upvote 0
The code to build a file name using the date and time in A1 depends on whether A1 is formatted to the required date and time format (noting that characters like ":" and "/" aren't allowed in file names) or is unformattted.

Change the following line:

VBA Code:
        wbNewFileName = targetWb.Worksheets(1).Range("A1").Value & targetWb.Worksheets(1).Range("B3").Value & Mid(wbFileName, InStrRev(wbFileName, "."))

To either:

VBA Code:
        wbNewFileName = targetWb.Worksheets(1).Range("A1").Text & " " & targetWb.Worksheets(1).Range("B3").Value & Mid(wbFileName, InStrRev(wbFileName, "."))
if A1 is formatted to the required date and time format.

Or:

VBA Code:
        wbNewFileName = Format(targetWb.Worksheets(1).Range("A1").Value, "yyyymmdd hhmmss") & " " & targetWb.Worksheets(1).Range("B3").Value & Mid(wbFileName, InStrRev(wbFileName, "."))
if A1 is unformatted - change the "yyyymmdd hhmmss" to the date and time format you want.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,107
Members
453,021
Latest member
Justyna P

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