Changing FilePath from user Select to a "fix" FilePath

TheHack22

Board Regular
Joined
Feb 3, 2021
Messages
121
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
profile_mask2.png

Imran Isshack​

12:24 PM (30 minutes ago)
to me






Hi All,

I have this VBA code I found on a MrExcel Thread which is working well, except I must manually select the target folder each time it’s executed.
This VBA renames all files in a folder based on a cell value(A2). Is there a way I can amend this so that it selects my intended folder automatically? I would like my filePath to be:
cleardot.gif

filepath = "C:\Users\C-TL\Box\Amp_Views\DataDownLoads\"


Sub RenameAllExcelFilesInDirectory()

With Application.FileDialog(msoFileDialogFolderPicker)
.Show
filepath = .SelectedItems(1)
End With

Set r = Workbooks.Add.Worksheets(1).Range("A2")
StrFile = Dir(filepath & "\*.*")

Do While Len(StrFile) > 0
strExtension = Split(StrFile, ".")(UBound(Split(StrFile, ".")))

Set WB = Workbooks.Open(filepath & "\" & StrFile)
StrNewfullfilename = WB.Sheets(1).Range("A2").Value & "." & strExtension
WB.Close

r.Value = StrFile 'print old name
r.Offset(, 1).Value = StrNewfullfilename 'print new name
Set r = r.Offset(1)
Name filepath & "\" & StrFile As filepath & "\" & StrNewfullfilename
StrFile = Dir
Loop
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Replace:

VBA Code:
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
filepath = .SelectedItems(1)
End With

with:

VBA Code:
filepath = "C:\Users\C-TL\Box\Amp_Views\DataDownLoads"
 
Upvote 0
Replace:

VBA Code:
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
filepath = .SelectedItems(1)
End With

with:

VBA Code:
filepath = "C:\Users\C-TL\Box\Amp_Views\DataDownLoads"
@jonnyl

Thanks very much for your response. I tried that and now it's breaking at this point:
Name filepath & "\" & StrFile As filepath & "\" & StrNewfullfilename

Error message run time error 5 "Invalid procedure call or argument"


Imran
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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