Set folder to rename

SunValley

New Member
Joined
Jul 12, 2023
Messages
45
Office Version
  1. 365
Platform
  1. Windows
After executing the code below, it is requested to define the path of the files through a dialog box. However, I need to replace this behavior, to make the file path predefined by me, instead of requesting the target folder manually.

VBA Code:
Sub RenameFiles_A()


Dim xDir As String

Dim xFile As String

Dim xRow As Long

With Application.FileDialog(msoFileDialogFolderPicker)

    .AllowMultiSelect = False

If .Show = -1 Then

    xDir = .SelectedItems(1)

    xFile = Dir(xDir & Application.PathSeparator & "*")

    Do Until xFile = ""

        xRow = 0

        On Error Resume Next

        xRow = Application.Match(xFile, Range("A:A"), 0)

        If xRow > 0 Then

            Name xDir & Application.PathSeparator & xFile As xDir & Application.PathSeparator & Cells(xRow, "C").Value

        End If

        xFile = Dir

    Loop

End If

End With

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi @SunValley and welcome to MrExcel board.

to make the file path predefined by me
How do you want to predefine it, directly in the macro or in a cell.

Try the following code by putting the path in the macro.
VBA Code:
xDir = "C:\trabajo\files"

If you want it from a cell, change this line: to something like this:
VBA Code:
xDir = range("A1").value
And then, change "A1" to the cell where you have the path

VBA Code:
Sub RenameFiles_B()
  Dim xDir As String, xFile As String
  Dim xRow As Long
  Dim ps As String
  
  xDir = "C:\trabajo\files"       'or range("A1").value
  xFile = Dir(xDir & Application.PathSeparator & "*")
  ps = Application.PathSeparator
  
  Do Until xFile = ""
    xRow = 0
    On Error Resume Next
    xRow = Application.Match(xFile, Range("A:A"), 0)
    If xRow > 0 Then
      Name xDir & ps & xFile As xDir & ps & Cells(xRow, "C").Value
    End If
    xFile = Dir
  Loop
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 1
Solution
How do you want to predefine it, directly in the macro or in a cell.
Directly in the macro

Sub RenameFiles_B()
Dim xDir As String, xFile As String
Dim xRow As Long
Dim ps As String

xDir = "C:\trabajo\files" 'or range("A1").value
xFile = Dir(xDir & Application.PathSeparator & "*")
ps = Application.PathSeparator

Do Until xFile = ""
xRow = 0
On Error Resume Next
xRow = Application.Match(xFile, Range("A:A"), 0)
If xRow > 0 Then
Name xDir & ps & xFile As xDir & ps & Cells(xRow, "C").Value
End If
xFile = Dir
Loop
End Sub
I replaced the path, however the rename is not applied, no error is displayed either
 
Upvote 0
I replaced the path, however the rename is not applied, no error is displayed either

No error is displayed, because in your macro you have this instruction:
On Error Resume Next
That instruction will ignore any errors.
Delete that line and try again.

Check the error message, it is possible that the path you entered does not exist, check that the path is correctly written and that the files with their names in column A exist in that path.

For me to help you more, you will have to put images of your path, of the files in the path and the names of the files in the sheet, that way I can review them and tell you where you have the error.
 
Upvote 0
I replaced the path
You don't help me much, you don't specify exactly what you changed, I'm going to assume you put the pah in the macro.

Try the following macro, it has validations to help you with your process.
VBA Code:
Sub RenameFiles_B()
  Dim xDir As String, xFile As String
  Dim ps As String
  Dim f As Range
  
  xDir = "C:\trabajo\files"       'or range("A1").value
  xFile = Dir(xDir & Application.PathSeparator & "*")
  ps = Application.PathSeparator
  
  If Dir(xDir, vbDirectory) = "" Then
    MsgBox "path does not exist"
    Exit Sub
  End If
  
  Do Until xFile = ""
    Set f = Range("A:A").Find(xFile, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      Name xDir & ps & xFile As xDir & ps & Cells(f.Row, "C").Value
    End If
    xFile = Dir
  Loop
End Sub
;)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
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