PDA

View Full Version : Need assistance pls



newbie2018
07-24-2018, 03:02 AM
i would like to know the code please, i have a folder with 10 sub-folders, and in the sub-folders around 500 invoice, the invoices name always starts with date (eg:20180101....), so i want to count all the invoices in certain month for each subfolder like this:enter image description here (https://i.stack.imgur.com/iR6wj.jpg) in Excel sheet

Thank you

georgiboy
07-24-2018, 05:07 AM
Welcome to the forum, how about:

Sub FindMonthFile()
Dim StrFile As String, objFSO
Dim mFold, sFold, x As Long
Dim fleCount As Long, hCell As Range
Dim rCell As Range, tmpMnth As String
x = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
mFolder = "C:\Users\A\Desktop\test\" 'change
Set mFold = objFSO.GetFolder(mFolder)
For Each sFold In mFold.sFolders
x = x + 1
StrFile = Dir(sFold & "\*.xls*") 'change
Set hCell = Cells(1, x)
hCell.Value = sFold.Name
'the 12 in the next line represents how many months in the table 'change
For Each rCell In Range(hCell.Offset(1, 0), hCell.Offset(12, 0)).Cells
tmpMnth = Range("B" & rCell.Row).Value
Do While Len(StrFile) > 0
If Mid(StrFile, 5, 2) = tmpMnth Then
fleCount = fleCount + 1
End If
StrFile = Dir
Loop
rCell.Value = fleCount
StrFile = Dir(sFold & "\*.xls*") 'change
fleCount = 0
Next rCell
Next
End Sub

Hope this helps

newbie2018
07-24-2018, 05:44 AM
Thank you for replaying George,
my code so far is like this :
Sub countthefiles()


Dim FolderPath As String, path As String, Count As Integer
FolderPath = "G:\Invoice Pool Unit4\Registration"


path = FolderPath & "\*.PDF"


Filename = Dir(path)


Do While Filename <> ""
Count = Count + 1
Filename = Dir()
Loop


Range("C3").Value = Count
'MsgBox count & " : files found in folder"

If Count = 0 Then

FolderPath = "G:\Invoice Pool \ File A\Archive"


path = FolderPath & "\*.PDF"


Filename = Dir(path)
End If

If InStr(file, "*201806") > 0 Then
MsgBox "found " & file
Exit Sub
End If

Do While Filename <> ""
Count = Count + 1
Filename = Dir()
Loop


Range("C3").Value = Count
'MsgBox count & " : files found in folder"
End Sub


But instead of counting only the files that starts with (201806) it actually counts all files.
where do you think my mistakes are ?

thank you again

georgiboy
07-24-2018, 08:56 AM
Have you tried the spreadsheet I have provided?

newbie2018
07-25-2018, 05:04 AM
i did yes, but somehow it didnt give me all i wanted, it kept counting other files too.
so im using this code at the moment, and trying to specify it to count only the files with name that starts with "201807"

Sub countthefiles()


Dim FolderPath As String, path As String, Count As Integer
FolderPath = "G:\Invoice Pool \For Registration"


path = FolderPath & "\*.PDF"


Filename = Dir(path)


Do While Filename <> "201807"
Count = Count + 1
Filename = Dir()
Loop

If Count >= 0 Then Range("L20").Value = Count

'MsgBox count & " : files found in folder"

End Sub



but it counts all the PDF files instead

georgiboy
07-25-2018, 05:36 AM
If you look at the code I provided you will see it returns the name of the folder or file and not the path, also it extracts the month from the middle of the file name.

My code would need to be pointed at a main folder that contains the subfolders that need to be searched. You would also need to point it at pdf and not xls* also.