Excel VBA Renaming Files to Cell Name

Dmack21

New Member
Joined
Jan 15, 2022
Messages
14
Good day,

Very new to visual basic and have had very little success attempting to achieve the following.

Ive got a series of *.dwg files saved in the same location as the spreadsheet named as follows:

STXX-XXX-X-BBBBBBB.dwg
STXX-XXX-X-CCCCCCC.dwg
STXX-XXX-X-DDDDDD.dwg

I would like the files renamed with STXX-XXX* being replaced with the contents of Cell D4 (Example "ST99-999").

ST99-999-X-BBBBBBB.dwg
ST99-999-X-CCCCCCC.dwg
ST99-999-X-DDDDDD.dwg

Would this be possible to achieve? If so, can I please get some guidance?

Thanks in advance.
 

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.
paste the code into a module,

in this example:
Range("B1").Value ,has the folder to use to rename files. (change or hardcode this folder valu)
Range("D4").Value , has the number to rename to.
then run: RenameAllFiles1Folder

Code:
Option Explicit
Sub RenameAllFiles1Folder()
Dim vDir
vDir = Range("B1").Value
If vDir = "" Then Exit Sub
Range("A3").Select
RenameAllFiles vDir
End Sub

Private Sub RenameAllFiles(ByVal pvDir)
Dim fso, oFolder, oFile, oRX
Dim sTxt As String, sFile As String
Dim vNewName, vMid, vNew, vPattern, vCellVal, vWord
Const kiNUM = 2
vCellVal = Range("D4").Value
vWord = "ST" & vCellVal & "-" & vCellVal
vPattern = ".xls"
'vPattern = ".dwg"
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(pvDir)
pvDir = FixDir(pvDir)
For Each oFile In oFolder.Files
 
  If vPattern = "" Then
     GoSub RenameIt
  ElseIf InStr(oFile.Name, vPattern) > 0 Then
     GoSub RenameIt
  End If
Next
Set oFile = Nothing
Set oFolder = Nothing
Set fso = Nothing
Exit Sub
RenameIt:
    vMid = Mid(oFile.Name, 9)
    vNewName = pvDir & vWord & vMid
  
    Name oFile As vNewName
      
       'track changes
    Debug.Print vNewName
    'ActiveCell.Value = vNewName
    'ActiveCell.Offset(1, 0).Select
Return
End Sub
Public Function FixDir(pvPath)
If pvPath = "" Then Exit Function
If Right(pvPath, 1) <> "\" Then pvPath = pvPath & "\"
FixDir = pvPath
End Function
 
Upvote 0
Solution
How about? Current names starting in A4 and replacements in D4

VBA Code:
Sub jec()
Dim it
For Each it In Range("A4", Range("A" & Rows.Count).End(xlUp))
  Name ThisWorkbook.Path & "\" & it As ThisWorkbook.Path & "\" & Left(it.Offset(, 3), 8) & Mid(it, 9)
Next
End Sub
 
Upvote 0
paste the code into a module,

in this example:
Range("B1").Value ,has the folder to use to rename files. (change or hardcode this folder valu)
Range("D4").Value , has the number to rename to.
then run: RenameAllFiles1Folder

Code:
Option Explicit
Sub RenameAllFiles1Folder()
Dim vDir
vDir = Range("B1").Value
If vDir = "" Then Exit Sub
Range("A3").Select
RenameAllFiles vDir
End Sub

Private Sub RenameAllFiles(ByVal pvDir)
Dim fso, oFolder, oFile, oRX
Dim sTxt As String, sFile As String
Dim vNewName, vMid, vNew, vPattern, vCellVal, vWord
Const kiNUM = 2
vCellVal = Range("D4").Value
vWord = "ST" & vCellVal & "-" & vCellVal
vPattern = ".xls"
'vPattern = ".dwg"
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(pvDir)
pvDir = FixDir(pvDir)
For Each oFile In oFolder.Files
 
  If vPattern = "" Then
     GoSub RenameIt
  ElseIf InStr(oFile.Name, vPattern) > 0 Then
     GoSub RenameIt
  End If
Next
Set oFile = Nothing
Set oFolder = Nothing
Set fso = Nothing
Exit Sub
RenameIt:
    vMid = Mid(oFile.Name, 9)
    vNewName = pvDir & vWord & vMid
 
    Name oFile As vNewName
     
       'track changes
    Debug.Print vNewName
    'ActiveCell.Value = vNewName
    'ActiveCell.Offset(1, 0).Select
Return
End Sub
Public Function FixDir(pvPath)
If pvPath = "" Then Exit Function
If Right(pvPath, 1) <> "\" Then pvPath = pvPath & "\"
FixDir = pvPath
End Function
Hi ranman,

Thank you so much for the assistance!! Great skills...The above code worked well!

Regards.
 
Upvote 0
How about? Current names starting in A4 and replacements in D4

VBA Code:
Sub jec()
Dim it
For Each it In Range("A4", Range("A" & Rows.Count).End(xlUp))
  Name ThisWorkbook.Path & "\" & it As ThisWorkbook.Path & "\" & Left(it.Offset(, 3), 8) & Mid(it, 9)
Next
End Sub
Thank you for your input. I was unable to utilise this code, being a new user Its more than likely a user error. Ranmans solution has done the job.

Regards
 
Upvote 0
paste the code into a module,

in this example:
Range("B1").Value ,has the folder to use to rename files. (change or hardcode this folder valu)
Range("D4").Value , has the number to rename to.
then run: RenameAllFiles1Folder

Code:
Option Explicit
Sub RenameAllFiles1Folder()
Dim vDir
vDir = Range("B1").Value
If vDir = "" Then Exit Sub
Range("A3").Select
RenameAllFiles vDir
End Sub

Private Sub RenameAllFiles(ByVal pvDir)
Dim fso, oFolder, oFile, oRX
Dim sTxt As String, sFile As String
Dim vNewName, vMid, vNew, vPattern, vCellVal, vWord
Const kiNUM = 2
vCellVal = Range("D4").Value
vWord = "ST" & vCellVal & "-" & vCellVal
vPattern = ".xls"
'vPattern = ".dwg"
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(pvDir)
pvDir = FixDir(pvDir)
For Each oFile In oFolder.Files
 
  If vPattern = "" Then
     GoSub RenameIt
  ElseIf InStr(oFile.Name, vPattern) > 0 Then
     GoSub RenameIt
  End If
Next
Set oFile = Nothing
Set oFolder = Nothing
Set fso = Nothing
Exit Sub
RenameIt:
    vMid = Mid(oFile.Name, 9)
    vNewName = pvDir & vWord & vMid
 
    Name oFile As vNewName
     
       'track changes
    Debug.Print vNewName
    'ActiveCell.Value = vNewName
    'ActiveCell.Offset(1, 0).Select
Return
End Sub
Public Function FixDir(pvPath)
If pvPath = "" Then Exit Function
If Right(pvPath, 1) <> "\" Then pvPath = pvPath & "\"
FixDir = pvPath
End Function
Good day Ranman,

The above code is working great and being successfully utilised.

The above code successfully filters all *.dwg files and renames them. I am now attempting to only rename dwg files in the folder which begin with an "X".

Ive had many unsuccessfull hours thus far..

Thanks in advance.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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