I am getting a Compile error: Method or data member not found on the .GetFileName Sub…
- User
I am getting a Compile error: Method or data member not found on the .GetFileName
Sub RenameFile()
Dim rng As Range Dim cell As Range Dim fileName As String Dim filePath As String Dim processedFlag As String
Set rng = ActiveSheet.UsedRange.Columns("A:B")
For Each cell In rng.Cells
If cell.Column = 1 Then ' Check if the cell has a hyperlink If cell.Hyperlinks.Count > 0 Then filePath = cell.Hyperlinks(1).Address fileName = VBA.FileSystem.GetFileName(filePath) End If ElseIf cell.Column = 2 Then ' Check for the processed flag processedFlag = Trim(cell.Value) If processedFlag = "Processed" Then ' Rename the file with PRO prefix VBA.FileSystem.Rename filePath, VBA.FileSystem.GetParentFolderName(filePath) & "\PRO" & fileName End If End If Next cell
End Sub
- Assistant
The error is most likely coming from the line "fileName = VBA.FileSystem.GetFileName(filePath)". The correct method for getting the file name is "VBA.FileSystem.Name(filePath)" instead of "VBA.FileSystem.GetFileName(filePath)". Try changing that and see if it resolves the error.
- User
tried it and still getting the same error message
- Assistant
Another possible cause of the error is that the Microsoft Scripting Runtime library may not be enabled in your Excel VBA editor. To enable it, follow these steps:
- Go to the VBA editor by pressing Alt + F11.
- Go to Tools -> References.
- Look for "Microsoft Scripting Runtime" in the list of available references.
- Check the checkbox next to it to enable the reference.
- Click OK to save and close the References dialog.
If this does not solve the problem, try restarting Excel and then run the macro again.