Hi,
I need some help with the current VBA code I am using.
The aim is first to count the .xlsx files in a folder and state the number in a defined cell, then in a table below on the same sheet, it lists files from selected folders....
Counting the files is fine but when listing .xlsx files from a selected folder it lists all the files regardless of the type....
This is currently the code I'm using, wanted to see if there is a way to list .xlsx files only?
Sub Outstanding39()
'Count files from selected folder
Dim folder_path As String
Dim strtype As String
Dim totalfiles As Variant
strtype = "*.xlsx*"
folder_path = Worksheets("Data2").Cells(83, 2).Value
If Right(folder_path, 1) <> "\" Then folder_path = folder_path & "\"
totalfiles = Dir(folder_path & strtype)
Dim i As Integer
While (totalfiles <> "")
i = i + 1
totalfiles = Dir
Wend
Worksheets("Open").Cells(15, 7).Value = i
Worksheets("Open").Cells(15, 7).Select
'List Files from selected folder
Dim objFSO As Scripting.FileSystemObject
Dim objFile As Scripting.File
Dim objFolder As Scripting.Folder
Dim nextRow As Long
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Worksheets("Data2").Cells(83, 2).Value)
nextRow = Cells(Rows.Count, 2).End(xlUp).Row + 1
For Each objFile In objFolder.Files
Cells(nextRow, 2) = objFile.Name
Cells(nextRow, 16) = objFile.ParentFolder
nextRow = nextRow + 1
Next
End Sub
I am hoping someone is able to assist with doing this....
I need some help with the current VBA code I am using.
The aim is first to count the .xlsx files in a folder and state the number in a defined cell, then in a table below on the same sheet, it lists files from selected folders....
Counting the files is fine but when listing .xlsx files from a selected folder it lists all the files regardless of the type....
This is currently the code I'm using, wanted to see if there is a way to list .xlsx files only?
Sub Outstanding39()
'Count files from selected folder
Dim folder_path As String
Dim strtype As String
Dim totalfiles As Variant
strtype = "*.xlsx*"
folder_path = Worksheets("Data2").Cells(83, 2).Value
If Right(folder_path, 1) <> "\" Then folder_path = folder_path & "\"
totalfiles = Dir(folder_path & strtype)
Dim i As Integer
While (totalfiles <> "")
i = i + 1
totalfiles = Dir
Wend
Worksheets("Open").Cells(15, 7).Value = i
Worksheets("Open").Cells(15, 7).Select
'List Files from selected folder
Dim objFSO As Scripting.FileSystemObject
Dim objFile As Scripting.File
Dim objFolder As Scripting.Folder
Dim nextRow As Long
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Worksheets("Data2").Cells(83, 2).Value)
nextRow = Cells(Rows.Count, 2).End(xlUp).Row + 1
For Each objFile In objFolder.Files
Cells(nextRow, 2) = objFile.Name
Cells(nextRow, 16) = objFile.ParentFolder
nextRow = nextRow + 1
Next
End Sub
I am hoping someone is able to assist with doing this....