Rename files with VBA macro wildcard

doriannjeshi

Active Member
Joined
Apr 5, 2015
Messages
338
Office Version
  1. 365
Platform
  1. Windows
Hello,

I need to rename a couple of files in a folder with a macro that use variable and constants in their names:

2508202300 rpt_shijet_OH_SL_IT_st_30_d.xls
250820601 SL ON HAN PER U.xls

Basically I want to give them a constant name to be able to use them with another macro
to rename them without the numbers that get generated daily, result:

rpt_shijet_OH_SL_IT_st_30_d.xls
SL ON HAN PER U.xls

If the filenames are present what options do I have , can I delete the old files and replace them with new files ?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi doriannjeshi,

Instead of renaming the files (which I dare say will cause issues as the file names will be duplicated in the folder once the date is removed) why not just exclude the date in your other macro like so:

VBA Code:
Debug.Print Split("2508202300 rpt_shijet_OH_SL_IT_st_30_d.xls", " ")(1)

Regards,

Robert
 
Upvote 0
Solution
Hi Robert,
How does this command work ?
does it remove the numbers of the date to the space delimiter ?
 
Upvote 0
It splits a string by a given delimiter (a space in this case). As the function uses a zero based index the (1) tells it to return everything after the first space which is the date in this case.

Click here for a great overview of the SPLIT (which is a VBA only) function.
 
Upvote 1
How would i use this command here?

Windows("280820241028 rpt_M2_OH_SL_IT_Last_30_days.xls").Activate
Sheets("sheet2").Select
Columns("A:A").Select
Selection.Copy
 
Upvote 0
I'm not sure what you're trying to do 🤔

If the workbook is open and you want to copy Col. A from Sheet2 here's one way (I have also included the file name without the date in a message box):

VBA Code:
Option Explicit
Sub Macro1()

    Dim wb As Workbook
    
    Set wb = Workbooks("280820241028 rpt_M2_OH_SL_IT_Last_30_days.xls")
    
    MsgBox Split(wb.Name, " ")(1)
    
    wb.Sheets("Sheet2").Range("A:A").Copy

End Sub
 
Upvote 0
Hi Trebor,
Can I use this macro as part of a larger macro ?
which adds copying from another workbook with date to be removed as this one ?
And how do I open this file automatically and then copy paste the content?
 
Upvote 0
I'm not sure what you're trying to do 🤔

If the workbook is open and you want to copy Col. A from Sheet2 here's one way (I have also included the file name without the date in a message box):

VBA Code:
Option Explicit
Sub Macro1()

    Dim wb As Workbook
   
    Set wb = Workbooks("280820241028 rpt_M2_OH_SL_IT_Last_30_days.xls")
   
    MsgBox Split(wb.Name, " ")(1)
   
    wb.Sheets("Sheet2").Range("A:A").Copy

End Sub
this table is showing ( changed the report name to a lighter file)
 
Upvote 0
how to avoid this table

1724962955707.png
 
Upvote 0
Sorry but I'm lost :( I'm sure someone on the forum will be able to help :)
 
Upvote 1

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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