Option Explicit
'================================================
' MAIN MACRO - SORT PDF FOLDERS & GENERATE REPORT
'================================================
Sub SortAndGeneratePDFReport()
Dim ws As Worksheet
Dim fso As Object
Dim rootFolder As Object
Dim subFolder As Object
Dim fileItem As Object
Dim folderList As Collection
Dim dist As Object
Dim mainPath As String
Dim sourcePath As String
Dim destPath As String
Dim bucketName As String
Dim providerName As String
Dim folderName As String
Dim folderKey As Variant
Dim pdfCount As Long
Dim totalPDFs As Long
Dim totalFolders As Long
Dim movedFolders As Long
Dim skippedFolders As Long
Dim lastRow As Long
Dim detailRow As Long
Dim summaryRow As Long
Dim distRow As Long
Dim i As Long
Dim statusText As String
Dim errorText As String
Dim oldScreen As Boolean
Dim oldAlerts As Boolean
Set ws = ActiveSheet
Set fso = CreateObject("Scripting.FileSystemObject")
oldScreen = Application.ScreenUpdating
oldAlerts = Application.DisplayAlerts
On Error GoTo FatalError
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'Clear old reports only
ws.Range("C:S").ClearContents
'Report headings
ws.Range("C1:H1").Value = Array( _
"Provider Path", _
"Provider Name", _
"Subfolder Name", _
"PDF Count", _
"Destination Path", _
"Status")
ws.Range("J1:O1").Value = Array( _
"Provider", _
"Total Subfolders", _
"Total PDFs", _
"Moved", _
"Skipped", _
"Invalid Paths")
ws.Range("Q1:S1").Value = Array( _
"Provider", _
"PDFs Per Folder", _
"Number Of Folders")
detailRow = 2
summaryRow = 2
distRow = 2
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 1 To lastRow
mainPath = Trim(CStr(ws.Cells(i, "A").Value))
If mainPath <> "" Then
If fso.FolderExists(mainPath) Then
Set rootFolder = fso.GetFolder(mainPath)
providerName = rootFolder.Name
Set folderList = New Collection
Set dist = CreateObject("Scripting.Dictionary")
totalFolders = 0
totalPDFs = 0
movedFolders = 0
skippedFolders = 0
'Collect original subfolders
For Each subFolder In rootFolder.SubFolders
If Not IsPDFBucket(subFolder.Name) Then
folderList.Add subFolder.Path
End If
Next subFolder
'Process folders
For Each folderKey In folderList
sourcePath = CStr(folderKey)
folderName = fso.GetFolder(sourcePath).Name
pdfCount = 0
statusText = ""
destPath = ""
totalFolders = totalFolders + 1
'Count direct PDF files
For Each fileItem In fso.GetFolder(sourcePath).Files
If LCase(fso.GetExtensionName(fileItem.Name)) = "pdf" Then
pdfCount = pdfCount + 1
End If
Next fileItem
totalPDFs = totalPDFs + pdfCount
'Distribution count
If dist.Exists(CStr(pdfCount)) Then
dist(CStr(pdfCount)) = dist(CStr(pdfCount)) + 1
Else
dist.Add CStr(pdfCount), 1
End If
If pdfCount = 0 Then
statusText = "Skipped - No PDF"
skippedFolders = skippedFolders + 1
Else
If pdfCount = 1 Then
bucketName = "1 PDF"
Else
bucketName = pdfCount & " PDFs"
End If
destPath = fso.BuildPath(mainPath, bucketName)
'Create destination folder
If Not fso.FolderExists(destPath) Then
fso.CreateFolder destPath
End If
'Prevent overwriting existing folders
If fso.FolderExists( _
fso.BuildPath(destPath, folderName)) Then
statusText = "Skipped - Name Conflict"
skippedFolders = skippedFolders + 1
Else
On Error Resume Next
Err.Clear
fso.MoveFolder sourcePath, _
fso.BuildPath(destPath, folderName)
If Err.Number = 0 Then
statusText = "Moved Successfully"
movedFolders = movedFolders + 1
Else
statusText = "Error: " & Err.Description
skippedFolders = skippedFolders + 1
End If
Err.Clear
On Error GoTo FatalError
End If
End If
'Detailed report
ws.Cells(detailRow, "C").Value = mainPath
ws.Cells(detailRow, "D").Value = providerName
ws.Cells(detailRow, "E").Value = folderName
ws.Cells(detailRow, "F").Value = pdfCount
ws.Cells(detailRow, "G").Value = destPath
ws.Cells(detailRow, "H").Value = statusText
detailRow = detailRow + 1
Next folderKey
'Provider summary
ws.Cells(summaryRow, "J").Value = providerName
ws.Cells(summaryRow, "K").Value = totalFolders
ws.Cells(summaryRow, "L").Value = totalPDFs
ws.Cells(summaryRow, "M").Value = movedFolders
ws.Cells(summaryRow, "N").Value = skippedFolders
ws.Cells(summaryRow, "O").Value = 0
summaryRow = summaryRow + 1
'PDF quantity distribution
For Each folderKey In dist.Keys
ws.Cells(distRow, "Q").Value = providerName
ws.Cells(distRow, "R").Value = _
folderKey & " PDF(s)"
ws.Cells(distRow, "S").Value = dist(folderKey)
distRow = distRow + 1
Next folderKey
Else
'Invalid path report
ws.Cells(detailRow, "C").Value = mainPath
ws.Cells(detailRow, "H").Value = "Invalid Path"
ws.Cells(summaryRow, "J").Value = mainPath
ws.Cells(summaryRow, "O").Value = 1
detailRow = detailRow + 1
summaryRow = summaryRow + 1
End If
End If
Next i
'Format all reports
FormatPDFReports ws
Application.ScreenUpdating = oldScreen
Application.DisplayAlerts = oldAlerts
MsgBox "PDF SORTING COMPLETED!" & vbCrLf & vbCrLf & _
"Provider Reports Generated Successfully." & vbCrLf & _
"Check columns C:S for complete reports.", _
vbInformation, "PDF Folder Manager"
Exit Sub
FatalError:
Application.ScreenUpdating = oldScreen
Application.DisplayAlerts = oldAlerts
MsgBox "Process stopped: " & Err.Description, _
vbExclamation, "PDF Manager"
End Sub
'================================================
' IDENTIFY DESTINATION FOLDERS
'================================================
Private Function IsPDFBucket(ByVal folderName As String) As Boolean
Dim txt As String
If Right$(folderName, 5) = " PDFs" Then
txt = Left$(folderName, Len(folderName) - 5)
ElseIf Right$(folderName, 4) = " PDF" Then
txt = Left$(folderName, Len(folderName) - 4)
Else
Exit Function
End If
If Len(txt) > 0 Then
IsPDFBucket = IsNumeric(txt)
End If
End Function
'================================================
' REPORT FORMATTING
'================================================
Private Sub FormatPDFReports(ByVal ws As Worksheet)
Dim headers As Variant
Dim col As Variant
headers = Array("C1:H1", "J1:O1", "Q1:S1")
For Each col In headers
With ws.Range(CStr(col))
.Interior.Color = RGB(34, 139, 70)
.Font.Color = vbWhite
.Font.Bold = True
.HorizontalAlignment = xlCenter
End With
Next col
ws.Columns("C:S").AutoFit
ws.Columns("C").ColumnWidth = 35
ws.Columns("E").ColumnWidth = 25
ws.Columns("G").ColumnWidth = 40
ws.Columns("H").ColumnWidth = 25
ws.Columns("J:S").ColumnWidth = 20
End Sub
'================================================
' CREATE GREEN FIXED BUTTON
'================================================
Sub CreatePDFManagerButton()
Dim ws As Worksheet
Dim btn As Shape
Set ws = ActiveSheet
On Error Resume Next
Set btn = ws.Shapes("PDFManagerButton")
On Error GoTo 0
If btn Is Nothing Then
Set btn = ws.Shapes.AddShape( _
5, _
ws.Range("U2").Left, _
ws.Range("U2").Top, _
220, _
55)
btn.Name = "PDFManagerButton"
End If
With btn
.Placement = xlFreeFloating
.OnAction = "'" & ThisWorkbook.Name & _
"'!SortAndGeneratePDFReport"
.Fill.ForeColor.RGB = RGB(34, 139, 70)
.Line.Visible = False
.TextFrame.Characters.Text = _
"SORT & GENERATE REPORT"
.TextFrame.HorizontalAlignment = xlHAlignCenter
.TextFrame.VerticalAlignment = xlVAlignCenter
With .TextFrame.Characters.Font
.Bold = True
.Size = 11
.Color = vbWhite
End With
End With
MsgBox "PDF Manager Button Created!", _
vbInformation, "PDF Manager"
End Sub