bamaisgreat
Well-known Member
- Joined
- Jan 23, 2012
- Messages
- 831
- Office Version
- 365
- Platform
- Windows
When I run the code code below which copies the files name, date and size to my worksheet I am having a couple issues. I believe the code is suppose to be putting them in date modified from newest to oldest but for some reason it is not showing up like that. Also im getting a run time error 1004 all of the sudden.
VBA Code:
Sub ListFilesInFolder1()
Columns("A:E").ClearContents
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder
Dim FileItem As Scripting.file
SourceFolderName = "K:\Engineering\Job Release"
Set FSO = New Scripting.FileSystemObject
Set SourceFolder = FSO.GetFolder(SourceFolderName)
Range("A1:C1") = Array("File Name", "Date Last Modified", "File Size")
i = 2
For Each FileItem In SourceFolder.Files
Cells(i, 1) = FileItem.Name
Cells(i, 2) = FileItem.DateLastModified
Cells(i, 3) = FileItem.Size
i = i + 1
Next FileItem
Set FSO = Nothing
If ActiveSheet.AutoFilterMode = True Then
Else
ActiveSheet.Range("A1").AutoFilter
End If
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add2 Key:= _
Range("B1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveSheet.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub