Option Explicit
Sub CreateMonthlyFileFromDailyFiles()
Dim strSourceWorksheetName As String
Dim strMonthlyRollupWorksheetName As String
Dim lNextWriteRow As Long
Dim bBadInput As Boolean
Dim iMonth As Integer
Dim iYear As Integer
Dim sInput As String
Dim strFileDirectory As String
Dim iAnswer As Integer
Dim iVisibleWindows As Integer
Dim lX As Long
Dim strFileName As String
Dim bFound As Boolean
Dim lY As Long
Dim lDataRowCount As Long
Dim lLinesCopied As Long
Dim sOK As String
Dim sNoWorksheet As String
Dim sNoFile As String
Dim sPreface As String
Dim sReport As String
strSourceWorksheetName = "Flare & Vent Tracking"
strMonthlyRollupWorksheetName = "Monthly Rollup"
lNextWriteRow = 2
ThisWorkbook.Activate
On Error Resume Next
Application.DisplayAlerts = False
Worksheets(strMonthlyRollupWorksheetName).Delete
On Error GoTo 0
Application.DisplayAlerts = True
Worksheets.Add(before:=Sheets("Sheet1")).Name = strMonthlyRollupWorksheetName
bBadInput = True
Do While bBadInput
sInput = InputBox("Enter the Month to process (1-12), X to cancel.", "Enter Month", Format(Now(), "m"))
If UCase(sInput) = "X" Then GoTo End_Sub
If Not IsNumeric(sInput) Then sInput = 0
If CInt(sInput) >= 1 And CInt(sInput) <= 12 Then
iMonth = CInt(sInput)
bBadInput = False
End If
Loop
bBadInput = True
Do While bBadInput
sInput = InputBox("Enter the 4-digit Year to process, X to cancel.", "Enter Year", Format(Now(), "yyyy"))
If UCase(sInput) = "X" Then GoTo End_Sub
If Not IsNumeric(sInput) Then sInput = 0
If CInt(sInput) >= 1900 And CInt(sInput) <= 2100 Then
iYear = CInt(sInput)
bBadInput = False
End If
Loop
strFileDirectory = "H:\Morning Reports " & iYear & "\" & Format(DateSerial(2010, iMonth, 1), "mmm") & "\"
'For local testing
strFileDirectory = "C:\PAB\Morning Reports " & iYear & "\" & Format(DateSerial(2010, iMonth, 1), "mmm") & "\"
'Close other workbooks - they may be ones we want to process
'and we don't want to overwrite them.
If Windows.Count > 1 Then
iAnswer = MsgBox("Close other workbooks and continue?" & vbCrLf & _
" OK to close all other workbooks and continue, or" & vbCrLf & _
" Cancel to stop this macro.", vbOKCancel + _
vbDefaultButton1 + vbExclamation, "Continue ?")
End If
If iAnswer = vbCancel Then
GoTo End_Sub
Else
iVisibleWindows = Windows.Count
For lX = Windows.Count To 1 Step -1
If Windows(lX).Caption <> ThisWorkbook.Name Then
If Windows(lX).Visible Then
'if workbook modified user will get
'chance to save or cancel for each
Windows(lX).Close
End If
iVisibleWindows = iVisibleWindows - 1
End If
Next
End If
'See if user chose Cancel for any close requests
If iVisibleWindows > 1 Then
MsgBox "Other Excel workbooks are still open. " & _
"Close other workbooks and try again", , "Process Cancelled."
GoTo End_Sub
End If
ThisWorkbook.Worksheets(strMonthlyRollupWorksheetName).Range("A1").Value = "Date"
'Process daily workbooks
For lX = 1 To Day(DateSerial(iYear, iMonth + 1, 1) - 1) '1 To last day of the month
strFileName = strFileDirectory & iMonth & "-" & lX & "-" & CInt(Right(CStr(iYear), 2)) & ".xls"
If Dir(strFileName) <> "" Then
Workbooks.Open Filename:=strFileName
'Process file
bFound = False
For lY = 1 To Worksheets.Count
If Worksheets(lY).Name = strSourceWorksheetName Then
bFound = True
Exit For
End If
Next
If bFound Then
'Copy header if not already copied
If ThisWorkbook.Worksheets(strMonthlyRollupWorksheetName).Range("B1").Value = "" Then
Worksheets(strSourceWorksheetName).Range("A3:T3").Copy _
Destination:=ThisWorkbook.Worksheets(strMonthlyRollupWorksheetName).Range("B1")
End If
'Copy data
Worksheets(strSourceWorksheetName).Activate
Range("A1").Select 'in case workbook was saved with an object selected
lDataRowCount = Application.WorksheetFunction.CountA(Worksheets(strSourceWorksheetName).Range("A5:A25"))
If lDataRowCount > 0 Then
Worksheets(strSourceWorksheetName).Range("A5:T" & 4 + lDataRowCount).Copy _
Destination:=ThisWorkbook.Sheets(strMonthlyRollupWorksheetName).Cells(lNextWriteRow, 2)
With ThisWorkbook.Sheets(strMonthlyRollupWorksheetName)
.Range(.Cells(lNextWriteRow, 1), .Cells(lNextWriteRow + lDataRowCount - 1, 1)).Value = _
Format(DateSerial(iYear, iMonth, lX), "mm/dd/yy")
End With
End If
lLinesCopied = lLinesCopied + lDataRowCount
'Write Date+rows copied to status report
sOK = sOK & vbLf & iMonth & "-" & lX & "-" & CInt(Right(CStr(iYear), 2)) & ".xls" & " " & vbTab & lDataRowCount & " events"
Else
'Write date + "source worksheet not found
sNoWorksheet = sNoWorksheet & vbLf & iMonth & "-" & lX & "-" & CInt(Right(CStr(iYear), 2)) & ".xls"
End If
ActiveWorkbook.Saved = True
ActiveWorkbook.Close
Else
'Write Missing Date to status report
sNoFile = sNoFile & vbLf & iMonth & "-" & lX & "-" & CInt(Right(CStr(iYear), 2)) & ".xls"
End If
lNextWriteRow = ThisWorkbook.Worksheets(strMonthlyRollupWorksheetName).Cells(Rows.Count, 1).End(xlUp).Row + 1
Next
With ThisWorkbook.Worksheets(strMonthlyRollupWorksheetName)
.Columns("A:U").EntireColumn.AutoFit
.Range("A1").Activate
End With
sPreface = Format(DateSerial(iYear, iMonth, 1), "mmmm yyyy") & " Daily Event Rollup Report" & vbLf
If sOK <> "" Then sReport = sOK & vbLf & vbTab & vbTab & "------" & vbLf & _
"Events Copied" & vbTab & lLinesCopied
If sNoWorksheet <> "" Then sReport = sReport & vbLf & vbLf & _
"The following workbooks did not contain a worksheet named '" & _
strSourceWorksheetName & "'" & vbLf & sNoWorksheet
If sNoFile <> "" Then sReport = sReport & vbLf & vbLf & _
"The following workbooks did not exist in" & vbLf & _
strFileDirectory & vbLf & sNoFile
Sheets(strMonthlyRollupWorksheetName).Copy
MsgBox sPreface & sReport
On Error Resume Next
If Len(sOK) > 0 Then
ActiveWorkbook.SaveAs Filename:= _
strFileDirectory & "Monthly Report as of " & Format(Now(), "m-d-yy") & ".xls", _
FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
Else
MsgBox "No daily files in " & strFileDirectory
End If
On Error GoTo 0
With ThisWorkbook.Worksheets(strMonthlyRollupWorksheetName)
.UsedRange.Clear
For lX = .Shapes.Count To 1 Step -1
.Shapes(lX).Delete
Next
End With
End_Sub:
End Sub