Macro to rename workbook

kaffal

Board Regular
Joined
Mar 7, 2009
Messages
68
Hi all, Everyday I receive 20 workbooks, named as date_xxx_xxx.wk1, where xx represent some random number. Example : 09032009_23_345.wk1.
These files are stored in a folder which contain all the files received for a month.

I need to rename them on a daily basis. I have 3 cell in my worksheet.
Cell A1: C:\Report ( location of the folder)
Cell A2: 09032009 ( a search criteria for file name)
Cell A3: mb ( location , which I can change )

When I run the macro. It will pick up Cell A1 as the folder to work on. Followed by It will search through the folder for files name containing Cell A2. Then It is found, It will pick up the cell value B1 in the current worksheet.
lastly it will rename the files as "Cell A2_CellA3_CellB1.xls" . For example if the cell B1 in the workbook is Car. The file name will be "09032009_mb_Car.xls".
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi all I attached a sample of my code. Can help me to do the code after each workbooks is open? To rename the file as "fcriteria_Tabname.xls"

Code:
fpath = Sheets("menu").Range("D3")
fcriteria = Sheets("menu").Range("D4")
TabNam = Range("D2").Value
   
    With Application.FileSearch
        .NewSearch
        .LookIn = fpath
        .FileType = msoFileTypeExcelWorkbooks
        .FileName = fcriteria & "*.xls"
    

            If .Execute > 0 Then 'Workbooks in folder

              For lCount = 1 To .FoundFiles.Count 'Loop through all.
                 'Open Workbook x and Set a Workbook variable to it
                 Set wbResults Workbooks.Open(FileName:=.FoundFiles(lCount), UpdateLinks:=0)


                  TabName = TabNam & Range("A1").Value
                  'Do the File save here. With file name = fcriteria_Tabname.xls                               
              Next lCount
                 
            End If
            
            Sheets("Menu").Select

    End With
 
Upvote 0

Forum statistics

Threads
1,203,069
Messages
6,053,352
Members
444,655
Latest member
didr

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