Macro to Save workbook by saving with month and year in Cell E6

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,566
Office Version
  1. 2021
Platform
  1. Windows
I have the month end Month and Year in Cell E6

I have the file name below

David James Comm.Oct 2021 New deals Ver 1.1.xlsm

I would like a macro to save the file by first removing the month and year after the . and before "New" and to include the month and year in Cell E6 after the .

If E6 contains Nov 2021 then the new file should save as David James Comm.Nov 2021 New deals Ver 1.1.xlsm


Your assistance is most appreciated
 
Many thanks for the help. The Macro now gives me a run time error

I tried the formula below in F6 and get #Value!

Code:
 =GetDateFromString(E1)

i have uploaded my file on Drop Box to make it easier when you look at what I am doing wrong

kindly check & advise

 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Never had a Dropbox account. Usually when people share I don;t have to login or do anything. Just download. Not sure how in this case since it is asking for login/password.

I'm confused. The Function is to be called by Sub Test, not as formula itself through worksheet. I thought you want to put the macro in workbook
David James Comm.Oct 2021 New deals Ver 1.1.xlsm

Then you will have Nov 2021 (string) in E6. You run the Sub Test. Then it will SaveAs
David James Comm.Nov 2021 New deals Ver 1.1.xlsm

The original file will still be there.
 
Upvote 0
Your date is not in text but Date format. So, I used to capture as displayed on sheet. The Date is located at E1, not E6 as initially stated. I also have double declared parameter previously causing an error. Now your Date is in format Oct-2021. So, I modified pattern to capture both Oct 2021 and Oct-2021.

This should work
VBA Code:
Sub Test()

Dim oldFName As String, newFName As String
Dim wb As Workbook
Dim ws As Worksheet

Set wb = ActiveWorkbook
Set ws = wb.Sheets("Comm Sheet")  ' Modify if necessary

Application.ScreenUpdating = False

With ActiveWorkbook
    oldFName = .FullName
    newFName = GetDateFromString(ws, oldFName)
    .SaveAs Filename:=newFName
End With

End Sub

Public Function GetDateFromString(ws As Worksheet, inputString As String) As String

Dim RegEx As Object

Set RegEx = CreateObject("VBScript.RegExp")
GetDateFromString = ""

With RegEx
    .Global = True
    .Pattern = "\.[A-Z][a-z]{2}[\s-]\d{4}"
    GetDateFromString = .Replace(inputString, "." & ws.Range("E1").Text)
End With

End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,215,551
Messages
6,125,478
Members
449,233
Latest member
Deardevil

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