Good morning, I would like to rename PDF files via Excel list, but by entering the following formula, it renames the files but not in the correct order, can you help me? Thanks in advance
‘=========>>
Option Explicit
‘———>>
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim RngTabella As Range
Dim oFso As Object
Dim oFolder As Object
Dim oFiles As Object
Dim oFile As Object
Dim sPath As String, sStr As String, sName As String
Dim i As Long, j As Long, k As Long
Dim iCtr As Long, jCtr As Long
Const sPercorso As String = _
"C:UsersmdiblasiDesktopLETTERE PDR CVB"
Const sFoglio As String = "Foglio1"
Const sTabella As String = "A1:A225"
Const sExt As String = ".PDF"
Set WB = ThisWorkbook
Set SH = WB.Sheets(sFoglio)
Set RngTabella = SH.Range(sTabella)
sStr = Application.PathSeparator
If Right(sPercorso, 1) = sStr Then
sPath = sPercorso
Else
sPath = sPercorso & sStr
End If
Set oFso = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFso.GetFolder(sPath)
Set oFiles = oFolder.Files
For Each oFile In oFiles
With oFile
If UCase(Right(.Name, 4)) = sExt Then
iCtr = iCtr + 1
sName = RngTabella.Cells(iCtr, 1) & "_" _
& RngTabella.Cells(iCtr, 2).Value
.Name = sName & sExt
End If
End With
Next oFile
Call MsgBox( _
Prompt:=iCtr & " File sono stati renominati", _
Buttons:=vbInformation, _
Title:="REPORT")
End Sub
‘<<=========
Manuela is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.