jmcginley3
New Member
- Joined
- Mar 28, 2018
- Messages
- 14
I have this macro which is the second half of my process to 1) pull in all the names of the files in a user selected folder and display them in column A and then 2) rename all those files to whatever the user inputs in column B on the spreadsheet. My problem is there are both .txt and .xlsx file types in the folder. I'm not sure how to account for the .txt files and make sure they remain .txt and aren't renamed to .xlsx. I know I'll need to make some sort of modification to the & ".xslx" portion or include some kind of If statement maybe? I'm pretty new to VBA, so any help would be greatly appreciated!
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Current File Name Pulled into Spreadsheet using a different macro.[/TD]
[TD]Updated File Name the user types into the spreadsheet prior to running the second macro (the macro I've pasted below)[/TD]
[TD]What the desired result should be.[/TD]
[/TR]
[TR]
[TD]PartNumbers858fm_47838_fmf.TXT[/TD]
[TD]PartNumbers_08292018[/TD]
[TD]<-- When I run the macro, this should save this file name and retain the .TXT extension (instead of adding a .XLSX extension)[/TD]
[/TR]
[TR]
[TD]StoreNumbers583_573fm_3j.XLSX[/TD]
[TD]StoreNumber_08292018[/TD]
[TD]<-- When I run the macro, this should save this file name and retain the .XLSX extension (this is currently working with the macro)[/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Current File Name Pulled into Spreadsheet using a different macro.[/TD]
[TD]Updated File Name the user types into the spreadsheet prior to running the second macro (the macro I've pasted below)[/TD]
[TD]What the desired result should be.[/TD]
[/TR]
[TR]
[TD]PartNumbers858fm_47838_fmf.TXT[/TD]
[TD]PartNumbers_08292018[/TD]
[TD]<-- When I run the macro, this should save this file name and retain the .TXT extension (instead of adding a .XLSX extension)[/TD]
[/TR]
[TR]
[TD]StoreNumbers583_573fm_3j.XLSX[/TD]
[TD]StoreNumber_08292018[/TD]
[TD]<-- When I run the macro, this should save this file name and retain the .XLSX extension (this is currently working with the macro)[/TD]
[/TR]
</tbody>[/TABLE]
Code:
Sub renamefiles()
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'USER SELECTS LOCATION TO SAVE RENAMED FILES
MsgBox "Select the location to save the renamed files."
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'USER CANCELS MESSAGE BOX
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'RENAME FILES TO THE VALUES IN COLUMN B
r = 2
Do Until IsEmpty(Cells(r, 1)) And IsEmpty(Cells(r, 2))
Name myPath & Cells(r, 1).Value As myPath & Cells(r, 2).Value & ".xlsx"
r = r + 1
Loop
'PROCESS COMPLETE
MsgBox "Done."
ResetSettings:
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub