VBA: Rename File When Excel is still/Keep Opening

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA: Rename File When Excel is still/Keep Opening

Post by HansV »

I have Excel 2021 too...
Best wishes,
Hans

snb
4StarLounger
Posts: 574
Joined: 14 Nov 2012, 16:06

Re: VBA: Rename File When Excel is still/Keep Opening

Post by snb »

You can use this code in any Workbook.
You can use a file containing this code as a template for any new workbook.

Code: Select all

Private Sub Workbook_Open()
  c00 = CreateObject("scripting.filesystemobject").getbasename(ThisWorkbook.FullName)
  ThisWorkbook.SaveAs ThisWorkbook.Path & "\" & IIf(InStr(c00, "_"), Left(c00, InStr(c00, "_")) & Format(Val(Right(c00, 3)) + 1, "000"), c00 & "_000"), ThisWorkbook.FileFormat
End Sub

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA: Rename File When Excel is still/Keep Opening

Post by HansV »

Susant wrote that they want to use the code in an add-in.
Best wishes,
Hans