VBA - Pull file pathway from cell H1

The Power Loon

New Member
Joined
Feb 7, 2020
Messages
34
Office Version
  1. 365
Platform
  1. Windows
The attached macro renames files in a given folder.

When starting, I get a prompt to select the folder in question. I am looking to remove this step, and instead use the pathway specified in cell H1. However, I have been unsuccessful in my attempts to do so.

How do I remove the pathway selection step in the macro below and instead use the pathway listed in cell H1?

VBA Code:
Sub RenameFiles()

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, "B").Value
        End If
        xFile = Dir
    Loop
End If
End With

End Sub

Thank you for your consideration of this question
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Thanks for posting on the forum.

Try this:

VBA Code:
Sub RenameFiles()
  Dim xDir As String
  Dim xFile As String
  Dim xRow As Long
 
  xDir = Range("H1")
  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, "B").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.
Sincerely
Dante Amor:)
--------------
 
Upvote 0
Solution
Thanks for posting on the forum.

Try this:

VBA Code:
Sub RenameFiles()
  Dim xDir As String
  Dim xFile As String
  Dim xRow As Long
 
  xDir = Range("H1")
  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, "B").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.
Sincerely
Dante Amor:)
--------------
This worked perfectly. Thank you for your help on this issue.
 
Upvote 1

Forum statistics

Threads
1,214,552
Messages
6,120,172
Members
448,948
Latest member
spamiki

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