Macro to Save File under New Name with space before month

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,566
Office Version
  1. 2021
Platform
  1. Windows
I have the following workbook below


Code:
 BR1 Purchase_Invoices lissting Aug-2022.xlsm

I have the following code to change the month and year based on the month and year in cell D2 on sheet "Recon"


When the mcro is run , the name is changed to

Code:
 BR1 Purchase_Invoices lisstingSep-2022.xlsm

I would like it to have space before the month and year for eg


Code:
 BR1 Purchase_Invoices lissting Sep-2022.xlsm

Code:
 Sub Save_fileunderMonth()

     Dim strNameToSave As String
    Dim strFullNm   As String
    Dim lPos        As Long
    Dim s           As String

  strNameToSave = Format(ThisWorkbook.Worksheets("Recon").Range("D2").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
End Sub


It would be appreciated if someone could kindly amend my code
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try this:

VBA Code:
Sub Save_fileunderMonth()
  Dim strNameToSave As String
  Dim strFullNm   As String
  Dim s           As String
  
  strNameToSave = Format(ThisWorkbook.Worksheets("Recon").Range("D2").Value, "mmm-yyyy")
  strFullNm = ThisWorkbook.FullName
  
  If InStr(1, strFullNm, strNameToSave, vbTextCompare) = 0 Then
    s = Left(strFullNm, Len(strFullNm) - Len(strNameToSave) - 5) & " " & strNameToSave
    ThisWorkbook.SaveAs s, 52
  End If
End Sub
 
Upvote 0
Solution
My apologies Dante. I thought that I had resoponded to you


Your code works perfectly
 
Upvote 0

Forum statistics

Threads
1,215,356
Messages
6,124,471
Members
449,163
Latest member
kshealy

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