Macro to Save File based on Month and year on sheet "Summary" U1

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have a workbook called "BR1 PL Month End Recon Aug 2022"

I have the curreent month end on Sheet "Summary" U1 for eg Sept-2022

I have set up a macro to remove the previous month and year and replace it with the current month and Year on sheet "Summary" U1

Howver when running the macro, I get a run time error "Microsoft Excel cannot access the file...... "

The code below is highlighted


Code:
 ThisWorkbook.SaveAs Left(strFullNm, Len(strFullNm) - 5), 52

See my code below

Code:
 Sub SaveWorkbookAsNewName()
    Dim strNameToSave As String
    Dim strFullNm   As String
    Dim lPos        As Long
    Dim s           As String

    strNameToSave = ThisWorkbook.Worksheets("summary").Range("U1").Value
    strFullNm = ThisWorkbook.FullName
    
    If InStr(1, strFullNm, strNameToSave, vbTextCompare) = 0 Then
        lPos = Len(strFullNm) - Len(strNameToSave) - 4
        Mid(strFullNm, lPos, Len(strNameToSave)) = strNameToSave

        ThisWorkbook.SaveAs Left(strFullNm, Len(strFullNm) - 5), 52
    End If
End Sub


It would be appreciated if someone could amend my code
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try using .text and not .value:
VBA Code:
strNameToSave = ThisWorkbook.Worksheets("summary").Range("U1").Text

Or, which should be my preferred choice:
VBA Code:
    strNameToSave = Format(ThisWorkbook.Worksheets("summary").Range("U1").Value, "mmm-yyyy")
Consider also using "yyyy-mm" as the format, because that would allow you to order the files by their file name
 
Upvote 0
I prefer using

Code:
 strNameToSave = Format(ThisWorkbook.Worksheets("summary").Range("U1").Value, "mmm-yyyy")


File savees with month and year, but file saved as a text file

I need it to save as an .xls file
 
Upvote 0
VBA Code:
ThisWorkbook.SaveAs Left(strFullNm, Len(strFullNm) - 5), 52
Using FileFormat=52 you should create an "xlsm" file, not a text file; if you need an "xls" format, then check the documentation to see which is the XlFileFormat enumeration to be used: XlFileFormat enumeration (Excel)
 
Upvote 0
The name of the current file is

Code:
 BR1 Sales_Ledger RECON(9.2.3) Aug 2022.xlsm

Cell U1 on Sheet Summary" is Sep-2022

I need the macro to change file name to


Code:
 BR1 Sales_Ledger RECON(9.2.3) Sep 2022.xlsm


However, when running the macro, it saves it as BR1 Sales_Ledger RECON(9.2.3) ASep-202


Kindly amend code accordingly
 
Upvote 0
VBA Code:
    strNameToSave = Format(ThisWorkbook.Worksheets("summary").Range("U1").Value, "mmm-yyyy")
    strFullNm = ThisWorkbook.FullName
  
    If InStr(1, strFullNm, strNameToSave, vbTextCompare) = 0 Then
        lPos = Len(strFullNm) - Len(strNameToSave) - 4
        Mid(strFullNm, lPos, Len(strNameToSave)) = strNameToSave
        ThisWorkbook.SaveAs Left(strFullNm, Len(strFullNm) - 5), 52
    End If
When I execute the above code, with the starting name = BR1 Sales_Ledger RECON(9.2.3) Sep 2022.xlsm, I got a file named BR1 Sales_Ledger RECON(9.2.3) ott-2022.xlsm

If you work with Office 2021 on a Windows platform and the macro is inserted into the vba project of the file you are reworking then I don't see why you get a different result; you need to excecute the macro step-by-step and check the intermediate results to understand what get wrong
 

Attachments

  • SAVEDAS_Immagine 2022-10-18 125314.jpg
    SAVEDAS_Immagine 2022-10-18 125314.jpg
    10.4 KB · Views: 4
Upvote 0
Solution
Many thanks for the help

I created .xlsm in Cell V1 on sheet "Summary" and joined cell U1 with cell V1 and then run the macro and it worked
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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