VBA file renamer. Please help!

timemoves

New Member
Joined
Apr 12, 2020
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
I've been running into a wall. I am trying to make a file renamer and have fallen flat on my face. I would like to remove the first 12 characters of a file (always the same) then delete everything after the part I want to keep" -" (always different after. It would be amazing if it utilized application.getopenfilename so I can use it in different folders and multiple files at the same time. Example of what I'm attempting below. Please keep in mind keep will always be different as will be everything after - .

Original:
123456789 - keep - 5654eg -5444_3453333 - vffgvxdrf6.csv

What I'm trying to rename to
Keep.csv
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Assuming the word you want to keep is always between the first and second hyphens in the file path and name and the file extension is always at the end, maybe this will get you started:

VBA Code:
Sub FileRenamer()
Dim fName As String, newName As String
fName = Application.GetOpenFilename
newName = Trim(Mid(WorksheetFunction.Substitute(fName, "-", WorksheetFunction.Rept(" ", 100)), 100, 100)) & Mid(fName, InStrRev(fName, "."))
MsgBox newName
'your code to open file named  fName then saveas newName using a folder of your choice
End Sub
 
Upvote 0
Here is a little User Defined Function that should do what you want:
VBA Code:
Function GetFileName(str As String) As String
    Dim temp As String
    temp = Mid(str, 13)
    GetFileName = Trim(Left(temp, InStr(temp, "-") - 1)) & ".csv"
End Function
Then you can call it from the worksheet or in VBA, like any other Excel function, like this:
VBA Code:
Sub Test()
    MsgBox GetFileName("123456789 - keep - 5654eg -5444_3453333 - vffgvxdrf6.csv")
End Sub
 
Upvote 0
Assuming the word you want to keep is always between the first and second hyphens in the file path and name and the file extension is always at the end, maybe this will get you started:

VBA Code:
Sub FileRenamer()
Dim fName As String, newName As String
fName = Application.GetOpenFilename
newName = Trim(Mid(WorksheetFunction.Substitute(fName, "-", WorksheetFunction.Rept(" ", 100)), 100, 100)) & Mid(fName, InStrRev(fName, "."))
MsgBox newName
'your code to open file named  fName then saveas newName using a folder of your choice
End Sub
It is always between the 2nd and 3rd hyphens. Your code works perfect otherwise. Any idea on how to do that? I really like your solution.
 
Upvote 0
It is always between the 2nd and 3rd hyphens. Your code works perfect otherwise. Any idea on how to do that? I really like your solution.
That's not consistent with the singular example you provided in your OP and your expected result is: keep.csv

In any case, this will return whatever is between the 2nd and 3rd hyphens:
VBA Code:
Sub FileRenamer()
Dim fName As String, newName As String
fName = Application.GetOpenFilename
newName = Trim(Mid(WorksheetFunction.Substitute(fName, "-", WorksheetFunction.Rept(" ", 100)), 200, 100)) & Mid(fName, InStrRev(fName, "."))
MsgBox newName
'your code to open file named  fName then saveas newName using a folder of your choice
End Sub
 
Upvote 0
That's not consistent with the singular example you provided in your OP and your expected result is: keep.csv

In any case, this will return whatever is between the 2nd and 3rd hyphens:
VBA Code:
Sub FileRenamer()
Dim fName As String, newName As String
fName = Application.GetOpenFilename
newName = Trim(Mid(WorksheetFunction.Substitute(fName, "-", WorksheetFunction.Rept(" ", 100)), 200, 100)) & Mid(fName, InStrRev(fName, "."))
MsgBox newName
'your code to open file named  fName then saveas newName using a folder of your choice
End Sub
PERFECT!
I know, for whatever reason I had it in my head that removing the first x digits then everything after the - was the best solution. I should have given a better example. I had to change it to 280 for whatever reason. Other than that it renames the files saving me a bunch of time. Now I'm going to play with it and try to get it to rename multiple files instead of one at a time.

Again, thanks!
 
Upvote 0
PERFECT!
I know, for whatever reason I had it in my head that removing the first x digits then everything after the - was the best solution. I should have given a better example. I had to change it to 280 for whatever reason. Other than that it renames the files saving me a bunch of time. Now I'm going to play with it and try to get it to rename multiple files instead of one at a time.

Again, thanks!
You are welcome - thanks for the reply.
 
Upvote 0

Forum statistics

Threads
1,214,829
Messages
6,121,826
Members
449,051
Latest member
excelquestion515

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