VBA Code for renaming filename base on month

luna007

New Member
Joined
Jan 6, 2021
Messages
10
Office Version
  1. 2010
Platform
  1. Windows
VBA Code for renaming filename base on month

I have monthwise excel files viz.,
Client1_Form_April
Client1_Form_May
Client1_Form_June
Client1_Form_July so…on for many clients

I want to rename these files if excel filename contains April then filename should be 01_ Client1_Form_April

If Excel filename contains May then filename should be 02_Client1_Form_May so…on Upto 12 months

I need a vba code for it with folder picker
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You can use a UDF to do the filename conversion. One example:
VBA Code:
Function MakeFileName(ByVal InputStr As String) As String
    Dim Client As String, ClientMonth As String, ClientNO As String, Ext As String
    Dim SPos As Long

    InputStr = Trim(InputStr)
    SPos = InStrRev(InputStr, ".")

    If SPos > 0 Then
        Ext = "." & Right(InputStr, Len(InputStr) - SPos)
        InputStr = Split(InputStr, ".")(0)
    Else
        Ext = ""
    End If
    ClientMonth = Split(Trim(InputStr), "_")(UBound(Split(Trim(InputStr), "_")))
    Client = Split(Trim(InputStr), "_")(0)
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "[^0-9]"
        ClientNO = Right("00" & .Replace(Client, ""), 2)
    End With

    MakeFileName = ClientNO & "_" & Client & "_Form_" & ClientMonth & Ext
End Function
VBA Code:
Sub TestRenamingUDF()
    Dim OldName As String, NewName As String, S As String

    OldName = "Client1_Form_April"
    NewName = MakeFileName(OldName)

    MsgBox OldName & " -> " & NewName
End Sub

This only addresses the topic/title of your post. I did not provide folder-picker code.
 
Upvote 0
Sorry, no. But there are many examples here on MrExcel. You can use the search feature to find them.
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,820
Members
449,469
Latest member
Kingwi11y

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