Function GetMonthNumber(monthAbbreviation As String) As Integer
Select Case LCase(monthAbbreviation)
Case "jan"
GetMonthNumber = 1
Case "feb"
GetMonthNumber = 2
Case "mar"
GetMonthNumber = 3
Case "apr"
GetMonthNumber = 4
Case "may"
GetMonthNumber = 5
Case "jun"
GetMonthNumber = 6
Case "jul"
GetMonthNumber = 7
Case "aug"
GetMonthNumber = 8
Case "sep"
GetMonthNumber = 9
Case "oct"
GetMonthNumber = 10
Case "nov"
GetMonthNumber = 11
Case "dec"
GetMonthNumber = 12
Case Else
GetMonthNumber = 0
End Select
End Function
Sub SelectFilesAndCopyToMasterSheetAdvanced()
Dim folderPath As String
Dim fileName As String
Dim filePath As String
Dim fileArr() As Variant
Dim fileCount As Long
Dim masterSheet As Worksheet
Dim lastRow As Long
Dim wb As Workbook
Dim ws As Worksheet
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a Folder"
If .Show = -1 Then
folderPath = .SelectedItems(1)
Else
Exit Sub
End If
End With
Range("A1:B" & Rows.Count).Clear
fileName = Dir(folderPath & "\*.*")
Do While fileName <> ""
If Not (GetAttr(folderPath & "\" & fileName) And vbDirectory) = vbDirectory _
And (LCase(Right(fileName, 4)) = ".xls" Or LCase(Right(fileName, 5)) = ".xlsx") Then
Dim dateStr As String
dateStr = Mid(fileName, InStrRev(fileName, "_") + 1, 9)
Dim fileDate As Date
Dim day As String
Dim month As String
Dim year As Integer
day = Left(dateStr, 2)
month = Mid(dateStr, 3, 3)
Dim yearStr As String
yearStr = Mid(dateStr, 6, 4)
If IsNumeric(yearStr) Then
year = CInt(yearStr)
Else
year = 1900
End If
fileDate = DateSerial(year, GetMonthNumber(month), CInt(day))
ReDim Preserve fileArr(1 To 2, 1 To fileCount + 1)
fileArr(1, fileCount + 1) = fileName
fileArr(2, fileCount + 1) = fileDate
fileCount = fileCount + 1
' Debug.Print dateStr
' Debug.Print day
' Debug.Print month
' Debug.Print GetMonthNumber(month)
' Debug.Print yearStr
' Debug.Print year
' Debug.Print fileDate
' Debug.Print "_________"
End If
fileName = Dir
Loop
Dim i As Long, j As Long
Dim tempName As Variant, tempDate As Variant
For i = 1 To fileCount - 1
For j = i + 1 To fileCount
If fileArr(2, j) < fileArr(2, i) Then
tempName = fileArr(1, i)
tempDate = fileArr(2, i)
fileArr(1, i) = fileArr(1, j)
fileArr(2, i) = fileArr(2, j)
fileArr(1, j) = tempName
fileArr(2, j) = tempDate
End If
Next j
Next i
Worksheets.Add(After:=Sheets(Sheets.Count)).Name = "Test"
Dim rowNum As Long
rowNum = 1
For i = 1 To fileCount
Sheets("Test").Cells(rowNum, 1).Value = fileArr(1, i)
Sheets("Test").Cells(rowNum, 2).Value = fileArr(2, i)
rowNum = rowNum + 1
Next i
Sheets("Test").Columns.AutoFit
Set masterSheet = ThisWorkbook.Sheets("Master Sheet")
lastRow = masterSheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
For i = 1 To fileCount
If LCase(Right(fileArr(1, i), 4)) = ".xls" Or LCase(Right(fileArr(1, i), 5)) = ".xlsx" Then
filePath = folderPath & "\" & fileArr(1, i)
Set wb = Workbooks.Open(filePath)
Set ws = wb.Sheets(1)
ws.Range("A1:C4").Copy masterSheet.Cells(lastRow, 2)
wb.Close SaveChanges:=False
lastRow = lastRow + 4
End If
Next i
masterSheet.Columns.AutoFit
End Sub