Sub ChangeWorksheetNames()
Dim FilePath As String
Dim FileExtension As String
Dim FileList As Variant
Dim Filename As String
Dim WSName As String
Dim Counter As Long
On Error GoTo ErrHandler
FilePath = "D:\XLSXTEMP\"
FileExtension = "xls*"
FileList = GetFileList(FilePath, FileExtension, False)
Dim xlAPP As Application
Set xlAPP = New Excel.Application
For Counter = LBound(FileList) To UBound(FileList)
DoEvents
Filename = FileList(Counter)
WSName = Left(Filename, InStrRev(Filename, ".") - 1)
Set wb = xlAPP.Workbooks.Open(FilePath & Filename)
' If wb.Sheets(1).Name Like "Sheet*" Then
wb.Sheets(1).Name = WSName
wb.Save
wb.Close False
'End If
Next
ErrHandler:
If Err.Number <> 0 Then
Debug.Print "[ERROR NO. " & Err.Number & "] " & Err.Description
Debug.Print "Filename: " & Filename
End If
xlAPP.Quit
Set xlAPP = Nothing
End Sub
Function GetFileList(FolderName As String, Optional FileExtension As String = "*", Optional FullPath As Boolean = False) As Variant
' Declares a dynamic array
Dim FileList() As String
Dim tmpFilename As String
Dim tmpPath As String
Dim Counter As Long
' Make any necessary corrections to the path
If Right(FolderName, 1) <> "\" Then FolderName = FolderName & "\"
If FullPath = True Then tmpPath = FolderName
tmpFilename = Dir(FolderName & "*." & FileExtension)
' Populate the dynamic array with filenames with the required file extension
Do While tmpFilename <> Empty
Counter = Counter + 1
ReDim Preserve FileList(1 To Counter)
FileList(Counter) = tmpPath & tmpFilename
tmpFilename = Dir
Loop
' Return the array
GetFileList = FileList
End Function