SaveAs .xlsm file as .xlsx with date&time as file name in current folder

shoheiyuna

New Member
Joined
Dec 3, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi, I would like to:
  1. SaveAs a current .xlsm file as .xlsx
  2. With the file name: yymmdd hhmm & "h" (e.g. 231204 1045h)
  3. In the same folder that the .xlsm file is in.
I have the macro and #1 and #3 works. However the file name keep ending up as FALSE.xlsx . I'm thinking the code in red is the issue but couldn't figure out the right code.
I've appended the code below. Appreciate if could point out what went wrong / and how to fix it. Thank you!

VBA Code:
Sub SaveFile()
'
'   SaveFile Macro
'

    Dim wb As Workbook
    Set wb = Workbooks("Setup.xlsm")
    
    Dim dt As String, wbName As String
    dt =[COLOR=rgb(184, 49, 47)] Format(Now(), "yymmdd hhmm")[/COLOR]
    wbName = "h"
    
    Application.DisplayAlerts = False
    ActiveWorkbook.CheckCompatibility = False
    ActiveWorkbook.SaveAs Filename = ActiveWorkbook.Path & Application.PathSeparator & dt & wbName, 51
    Application.DisplayAlerts = True
    ActiveWorkbook.CheckCompatibility = True
        


End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try the following:
VBA Code:
Sub SaveFile()
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs ActiveWorkbook.Path & "\" & Format(Now(), "yymmyy hhss") & "h.xlsx", FileFormat:=51
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Solution
Try the following:
VBA Code:
Sub SaveFile()
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs ActiveWorkbook.Path & "\" & Format(Now(), "yymmyy hhss") & "h.xlsx", FileFormat:=51
    Application.DisplayAlerts = True
End Sub

Thank you for the reply! Works perfectly. I've only amended slightly so that the date format is yymmmdd hhmm. Full code below:

VBA Code:
Sub SaveFile()
'
'   SaveFile Macro
'

    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs ActiveWorkbook.Path & "\" & Format(Now(), "yymmdd hhmm") & "h.xlsx", FileFormat:=51
    Application.DisplayAlerts = True
       


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
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