Sub ListFiles()
Application.ScreenUpdating = False
Range("a5:b65536").ClearContents
Range("a5").Select
Dim fileList() As String
Dim fName As String
Dim fPath As String
Dim I As Integer
Dim myfind As Integer, mylen As Integer, mynum As Integer
fPath = Range("A1")
fName = Dir(fPath & "*.*")
While fName <> ""
I = I + 1
ReDim Preserve fileList(1 To I)
fileList(I) = fName
fName = Dir()
Wend
If I = 0 Then
MsgBox "No files found"
Exit Sub
End If
For I = 1 To UBound(fileList)
myfind = WorksheetFunction.Find(".", fileList(I), 1) - 1
mylen = Len(fileList(I))
mynum = mylen - myfind
If Range("b1") = "x" Then
Range("A" & I + 4).Value = "=hyperlink(""" & fPath & fileList(I) & """)"
Range("A1").Select
Else
Range("A" & I + 4).Value = Left(fileList(I), myfind)
Range("B" & I + 4).Value = Right(fileList(I), mynum)
Range("A:A").Select
Selection.Font.ColorIndex = 1
Selection.Font.Underline = xlUnderlineStyleNone
Range("A1").Select
End If
Next
ActiveSheet.Cells.EntireColumn.AutoFit
Dim NewName As String, FileName As String, OldName As String
With ActiveSheet
OldName = .Name 'Get the sheet name which will be changed
FileName = .Cells(1, 1).Value 'Get the file name from which to generate a new sheet name
End With
NewName = Left(FileName, InStrRev(FileName, "\") - 1) 'Drops the ending "\" from the file name. The filename is now "X:\XXX\XXX\...\Name"
NewName = Mid(NewName, Len(Left(NewName, InStrRev(NewName, "\"))) + 1) 'Takes all the characters following the last "\"
Worksheets(OldName).Name = NewName 'Renames sheet
Application.ScreenUpdating = True
End Sub