I am using these two codes:
- Get_Files_Information: To pull up the file names from the folder for renaming
Option Explicit
Sub Get_Files_Information()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")
Dim fso As New FileSystemObject
Dim fo As Folder
Dim f As File
Set fo = fso.GetFolder(sh.Range("H1").Value)
Dim last_raw As Integer
For Each f In fo.Files
last_raw = sh.Range("A" & Application.Rows.Count).End(xlUp).Row + 1
sh.Range("A" & last_raw).Value = f.Name
Next
MsgBox "Done"
End Sub
- Rename_Files: This code is to rename the file
Sub Rename_Files()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")
Dim fso As New FileSystemObject
Dim fo As Folder
Dim f As File
Dim new_name As String
Set fo = fso.GetFolder(sh.Range("H1").Value)
For Each f In fo.Files
new_name = Application.VLookup(f.Name, sh.Range("A:B"), 2, 0)
f.Name = new_name
Next
MsgBox "Done"
End Sub
When the Get_Files_Information is fetching the file name the result is coming with file extension. I want to exclude the file extension from the file name so that the renaming will not get stuck due to extension of the file.
Also when executing the rename code I get
Type Mismatch Runtime error 13.
on new_name = Application.VLookup(f.Name, sh.Range("A:B"), 2, 0)
Excel macro file for reference.
https://drive.google.com/open?id=1Zivo3aIn-Id9XtgQu-qpOstL_j7eacjv
Debug.Printto see, what your Lookup returns?