Sheet Renameing which has a date

Kayslover

Board Regular
Joined
Sep 22, 2020
Messages
168
Office Version
  1. 2013
Platform
  1. Windows
Hi,
I was searching the Internet to automate the renaming of sheets by referencing a cell in each sheet, and I came across the following code which have been made into a macro and stored in a workbook called Master:-

VBA Code:
Sub RenameSheets()

Dim ws As Worksheet

On Error GoTo err_chk

For Each ws In Worksheets

If Len(Trim(ws.Range("B4"))) > 0 Then ws.Name = ws.Range("B4").Text

Next ws

On Error GoTo 0

Exit Sub

err_chk:

MsgBox "Error #:" & Err.Number & ": " & Err.Description, vbOKOnly, "ERROR RENAMING " & ws.Name

Err.Clear

Resume Next

End Sub

The formatting of cell B4 is dd-mmm-yy, Is there a way that I can format the sheet name to be in dd-mm-yy format?

I have a sheet called Totals and I would like cell A2 to become the 1st of the Month of the year entered in B4 sheet1 (i.e. if B4 is 03-Jan-2021, then A2 to become 01-01-23)

Additionally, I would like to save the workbook as 01 January, 02 February, 03 March etc. (i.e. month number followed by the full month description) depending on the value in cell B4 in sheet1.

Example if B4 in sheet1 has a date of Jan 2023, then the workbook should be named 01 January, if B4 in sheet1 is Feb 2023, then the workbook should be named 02 February etc, etc etc.

The workbooks should be stored in the same folder that workbook Master is in.

Rather than running a macro can something be done to automatically achieve the above once a date has been put into cell B4 on Sheet1?

Any assistance offered would be gratefully received.
 
Last edited by a moderator:
OK, now I understand

I have the following after the DIM statement:-

path="D:\Till Takings\2023\"

I then have ActiveWorkbook.SaveAs Filename:=Path & "01 January.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled.

This works a treat and saves the files as defined by Path.

However, rather than hard coding the path, is there any way of extracting it from the location of where the template is stored (i.e."D:\Till Takings\2023\", in 2023, it will become "D:\Till Takings\2024\" in 2024) etc.etc etc.

The following formula gives me the exact location of the place that the template is in:-

=SUBSTITUTE( LEFT(CELL("filename",A1),FIND("]",CELL("filename",A1))-12),"[","")

I need to somehow have this in the macro.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
If the macro is within the file (template) which is in that directory then:
path=ThisWorkbook.Path & "\" should work nicely.
 
Upvote 0
You can also:
path=range("A1") 'wherever you put the formula for the path
But this workbook.path method is the more direct way.
 
Upvote 0
I have replaced the hard coded path with Path = ThisWorkbook.Path & "\"

The ActiveWorkbook.SaveAs Filename:=Path & "01 January.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled

However, when I run the macro to copy the sheets, it seems to be doing nothinh. What I mean by that it was previously, creating files called 01 January, 02 February etc to the hard code path, now no files are created.

Any ideas?
 
Upvote 0
Double clicked on the template and it created a workbook called Master1.xlsm. Ran the macro to rename the sheets and ran the second macro to save files to 01 January, 02 February etc.

This macro failed, and I had to manually save Master1.xlsm to the appropriate folder.

Ran the second macro to save files to 01 January, 02 February etc. and it created 01 January, 02 February as expected.

I changed the first macro which renames the sheet names to save Master1.xlsm to the appropriate folder (along with the code Path = ThisWorkbook.Path & "\" and ActiveWorkbook.SaveAs Filename:=Path & "Master1.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled

But it does not save the workbook to the path.

I have to manually save where required and then run the second macro.

Not the perfect solution, but 99% there, so it will have to do.

Thanks for your assistance
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,378
Members
448,955
Latest member
BatCoder

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