Function ErrorMessage( _
pErrNum As Integer, _
psErrDescr, _
Optional psSubName = "", _
Optional psStepID = "")
Dim sMsg As String
Dim sTitle As String
sTitle = "Error Message"
sMsg = "Error #" & pErrNum & " occurred"
If psSubName <> "" _
Then sMsg = sMsg & Chr(10) & "in procedure " & psSubName
sMsg = sMsg & "."
If psStepID <> "" _
Then sMsg = sMsg & Chr(10) & "Step ID: " & psStepID & "."
sMsg = sMsg & Chr(10) & "Error Type: " & psErrDescr & "."
MsgBox sMsg, vbOKOnly + vbCritical, sTitle
Err.Clear
Application.StatusBar = False
DoEvents
End Function
Function GetFileName( _
ByRef psPathAndFile As String, _
ByVal psStartPath As String, _
Optional ByVal psFilterName As String = "Any file", _
Optional ByVal psFilter As String = "*.xl*?", _
Optional ByVal psTitle As String = "Select a file.")
Dim fdGetFileName As Office.FileDialog
Dim vAns As Variant
Set fdGetFileName = Application.FileDialog(msoFileDialogFilePicker)
DoGetFile:
With fdGetFileName
.Filters.Clear
.Filters.Add psFilterName, psFilter, 1
.Title = psTitle
.AllowMultiSelect = False
.InitialFileName = psStartPath
If .Show = True Then
psPathAndFile = .SelectedItems(1)
Exit Function
Else
vAns = MsgBox("Please select a file.", vbOKCancel + vbQuestion, "Selecting a folder containing PDFs to Print")
If vAns = vbOK Then GoTo DoGetFile
End If
End With
End Function
Function GetFormulasRowsData(ByRef pavAnchorRows() As Variant)
Dim iLastRow As Long
Dim iRow As Long
Dim iStationsCount As Long
Dim iStation As Long
ReDim pavAnchorRows(1 To 2, 1)
With Worksheets("Formulas")
iLastRow = .Range("B1").Cells(Rows.Count, 1).End(xlUp).Row
For iRow = 1 To iLastRow
If .Range("B1").Cells(iRow).value = "Recipe #" _
Then
iStationsCount = iStationsCount + 1
ReDim Preserve pavAnchorRows(1 To 2, iStationsCount)
pavAnchorRows(1, iStationsCount) = iRow
End If
Next iRow
For iStation = 1 To UBound(pavAnchorRows, 2)
iRow = 0
With .Range("A1").Cells(pavAnchorRows(1, iStation))
Do
iRow = iRow + 1
Loop Until Trim(.Offset(iRow)) = ""
pavAnchorRows(2, iStation) = iRow - 1
End With
Next iStation
End With
End Function
Function GetReportStationsData(ByRef pavNumberRows() As Variant, Optional ByVal iColOffset As Long = 0)
Dim iLastRow As Long
Dim iRow As Long
Dim iStation As Long
ReDim pavNumberRows(1 To 3, 1)
With Worksheets("Report")
iLastRow = .Range("A1").Cells(Rows.Count, 1).End(xlUp).Row
For iRow = 1 To iLastRow
If .Range("A1").Offset(0, iColOffset).Cells(iRow).value = "Recipe #" _
Then
iStation = iStation + 1
ReDim Preserve pavNumberRows(1 To 3, iStation)
pavNumberRows(1, iStation) = iRow + 1
End If
Next iRow
For iStation = 1 To UBound(pavNumberRows, 2)
iRow = 0
With .Range("A1").Cells(pavNumberRows(1, iStation))
Do
iRow = iRow + 1
Loop Until Trim(.Offset(iRow)) = ""
pavNumberRows(2, iStation) = iRow
pavNumberRows(3, iStation) = .Offset(-2, 1).value
End With
Next iStation
End With
End Function
Function GetStationNames()
Dim sStationsNamesList As String
Dim sStationName As String
Dim iStationsCount As Long
Dim iStation As Long
iStationsCount = 9
GetStationNames = "none"
For iStation = 1 To iStationsCount
sStationName = [Formulas].Range("Station" & iStation & "Name")
If UCase(sStationName) <> "NOT USED" And sStationName <> "" _
Then
If sStationsNamesList <> "" And iStation < iStationsCount _
Then sStationsNamesList = sStationsNamesList & ", "
sStationsNamesList = sStationsNamesList & Application.WorksheetFunction.Proper(sStationName)
End If
Next
GetStationNames = sStationsNamesList
End Function
Sub CreateFormulasTop()
Dim pPicTop As Picture
Dim sPictureName As String
sPictureName = "FormulasTop"
Dim sSubName As String
Dim sStepID As String
sSubName = "CreateFormulasTop"
sStepID = ""
On Error GoTo ErrHandler
On Error Resume Next
Worksheets("Formulas").Shapes(sPictureName).Delete
On Error GoTo ErrHandler
sStepID = "copying Formulas worksheet top as picture"
With Worksheets("Form Top")
.Range("FormulasTop").Copy
Set pPicTop = .Pictures.Paste
End With
sStepID = "formatting Formulas worksheet top picture"
With pPicTop.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
sStepID = "putting form picture into Formulas worksheet"
pPicTop.Cut
With Worksheets("Formulas")
.Activate
.Range("A1").Activate
Set pPicTop = .Pictures.Paste
pPicTop.Name = sPictureName
.Range("B19").Activate
End With
pPicTop.ShapeRange.IncrementLeft 3.3333070866
Application.CutCopyMode = False
Exit Sub
ErrHandler:
Call ErrorMessage(Err.Number, Err.Description, sSubName, sStepID)
End Sub
Sub ImportReportWorksheet()
Dim wsReportNew As Worksheet
Dim sPathAndFile As String
Dim sStartPath As String
Dim rCell As Range
Dim sSubName As String
Dim sStepID As String
sSubName = "ImportReportWorksheet"
sStepID = ""
On Error GoTo ErrHandler
sStepID = "1. setting path to the Report"
sStartPath = ThisWorkbook.Path
Application.DisplayAlerts = False
sStepID = "2. deleting existing Report worksheet"
On Error Resume Next
ThisWorkbook.Worksheets("Report").Delete
On Error GoTo ErrHandler
sStepID = "3. getting path and file for Report"
Call GetFileName(sPathAndFile, sStartPath, "Report File", "*.xl*")
sStepID = "4. opening and accessing Report workbook"
Workbooks.Open sPathAndFile
Set wsReportNew = ActiveWorkbook.Worksheets(1)
sStepID = "5. checking that worksheet is a Report"
With wsReportNew
Set rCell = .Cells.Find(What:="Report Parameters", _
After:=.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True)
If rCell Is Nothing _
Then
MsgBox "The worksheet imported is not a Report", vbCritical
ActiveWorkbook.Close
Exit Sub
End If
End With
sStepID = "6. importing Report worksheet"
wsReportNew.Move Before:=ThisWorkbook.Sheets(1)
sStepID = "7. naming and formatting Report worksheet"
ActiveSheet.Name = "Report"
Call ReformatReport
Exit Sub
ErrHandler:
Call ErrorMessage(Err.Number, Err.Description, sSubName, sStepID)
End Sub
Sub ReformatReport()
Dim wsReport As Worksheet
Dim wsLoop As Worksheet
Dim bReportSheetExists As Boolean
Dim sReportSheetName As String
Dim iRow As Long
Dim iStation As Long
Dim avNumberRows() As Variant
Dim iLastRow As Long
Dim vIsReportFormatted As Variant
Dim sPageHeaderText As String
Dim sRunLabel1 As String
Dim sRunLabel2 As String
Dim sRunLabel3 As String
Dim sSubName As String
Dim sStepID As String
sSubName = "ReformatReport"
sStepID = ""
On Error GoTo ErrHandler
Application.DisplayAlerts = False
sStepID = "1. checking Report worksheet exists"
sReportSheetName = "Report"
bReportSheetExists = False
For Each wsLoop In ThisWorkbook.Worksheets
If wsLoop.Name = sReportSheetName _
Then
bReportSheetExists = True
Exit For
End If
Next wsLoop
If Not bReportSheetExists _
Then
MsgBox "The worksheet named " & sReportSheetName & " was not found.", vbExclamation
Exit Sub
End If
sStepID = "2. initializations"
Set wsReport = ThisWorkbook.Worksheets(sReportSheetName)
iLastRow = wsReport.Range("A1").Cells(Rows.Count, 1).End(xlUp).Row
vIsReportFormatted = False
On Error Resume Next
vIsReportFormatted = wsReport.Names("HasBeenFormatted").Index <> (Err.Number = 0)
On Error GoTo ErrHandler
If vIsReportFormatted _
Then
MsgBox "The data has already been formatted.", vbInformation
Exit Sub
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
sStepID = "3. checking Report worksheet exists"
ActiveWindow.DisplayGridlines = False
With wsReport
sPageHeaderText = .Range("H1")
sRunLabel1 = .Range("Z1")
sRunLabel2 = .Range("Z2")
sRunLabel3 = .Range("Z3")
End With
sStepID = "4. removing cell borders and fill color"
With wsReport.Range("A1:AK20")
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeTop).LineStyle = xlNone
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
With wsReport.Rows("4:4").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With wsReport.Range("D1")
For iRow = 1 To iLastRow
If .Cells(iRow) <> "" Then .Cells(iRow).Offset(0, -1).value = .Cells(iRow).value
.Cells(iRow) = ""
Next iRow
End With
sStepID = "5. removing extra columns, aligning remaining"
With wsReport
.Columns("AK:AK").Delete Shift:=xlToLeft
.Columns("AJ:AJ").Delete Shift:=xlToLeft
With .Columns("AI:AI")
.Cells.HorizontalAlignment = xlCenter
.Cells.VerticalAlignment = xlBottom
End With
.Columns("AH:AH").Delete Shift:=xlToLeft
.Columns("AG:AG").Delete Shift:=xlToLeft
With .Columns("AF:AF")
.Cells.HorizontalAlignment = xlCenter
.Cells.VerticalAlignment = xlBottom
End With
.Columns("AE:AE").Delete Shift:=xlToLeft
.Columns("AD:AD").Delete Shift:=xlToLeft
.Columns("AC:AC").Delete Shift:=xlToLeft
With .Columns("AB:AB")
.Cells.HorizontalAlignment = xlCenter
.Cells.VerticalAlignment = xlBottom
End With
.Columns("AA:AA").Delete Shift:=xlToLeft
.Columns("Z:Z").Delete Shift:=xlToLeft
With .Columns("Y:Y")
.Cells.HorizontalAlignment = xlCenter
.Cells.VerticalAlignment = xlBottom
End With
.Columns("X:X").Delete Shift:=xlToLeft
With .Columns("W:W")
.Cells.HorizontalAlignment = xlCenter
.Cells.VerticalAlignment = xlBottom
End With
.Columns("V:V").Delete Shift:=xlToLeft
.Columns("U:U").Delete Shift:=xlToLeft
With .Columns("T:T")
.Cells.HorizontalAlignment = xlCenter
.Cells.VerticalAlignment = xlBottom
End With
.Columns("S:S").Delete Shift:=xlToLeft
With .Columns("R:R")
.Cells.HorizontalAlignment = xlCenter
.Cells.VerticalAlignment = xlBottom
End With
.Columns("Q:Q").Delete Shift:=xlToLeft
.Columns("P:P").Delete Shift:=xlToLeft
With .Columns("O:O")
.Cells.HorizontalAlignment = xlCenter
.Cells.VerticalAlignment = xlBottom
End With
.Columns("N:N").Delete Shift:=xlToLeft
With .Columns("M:M")
.Cells.HorizontalAlignment = xlCenter
.Cells.VerticalAlignment = xlBottom
End With
.Columns("L:L").Delete Shift:=xlToLeft
.Columns("K:K").Delete Shift:=xlToLeft
With .Columns("J:J")
.Cells.HorizontalAlignment = xlCenter
.Cells.VerticalAlignment = xlBottom
End With
.Columns("I:I").Delete Shift:=xlToLeft
.Columns("H:H").Delete Shift:=xlToLeft
With .Columns("G:G")
.Cells.HorizontalAlignment = xlCenter
.Cells.VerticalAlignment = xlBottom
End With
.Columns("F:F").Delete Shift:=xlToLeft
.Columns("E:E").Delete Shift:=xlToLeft
.Columns("D:D").Delete Shift:=xlToLeft
With .Columns("C:C")
.Cells.HorizontalAlignment = xlCenter
.Cells.VerticalAlignment = xlBottom
End With
.Columns("B:B").Delete Shift:=xlToLeft
End With
sStepID = "6. setting columns' width"
With wsReport
.Columns("A:A").EntireColumn.ColumnWidth = 8.5
.Columns("B:B").EntireColumn.ColumnWidth = 18
.Columns("C:C").EntireColumn.ColumnWidth = 10
.Columns("D:D").EntireColumn.ColumnWidth = 15.43
.Columns("E:E").EntireColumn.ColumnWidth = 7.5
.Columns("F:F").EntireColumn.ColumnWidth = 9.14
.Columns("G:G").EntireColumn.ColumnWidth = 7.9
.Columns("H:H").EntireColumn.ColumnWidth = 8.71
.Columns("I:I").EntireColumn.ColumnWidth = 8.71
.Columns("J:J").EntireColumn.ColumnWidth = 8.71
.Columns("K:K").EntireColumn.ColumnWidth = 8.57
.Columns("L:L").EntireColumn.ColumnWidth = 8.64
.Columns("M:M").EntireColumn.ColumnWidth = 8.64
End With
sStepID = "7. removing rows' merging"
wsReport.Rows.AutoFit
With wsReport.Range("A1")
For iRow = 1 To iLastRow
With .Cells(iRow)
.MergeCells = False
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
If .value = "Recipe #" _
Then
.HorizontalAlignment = xlCenter
.RowHeight = 24
End If
End With
Next iRow
End With
sStepID = "8. setting cells' vertical alignment"
With wsReport.Range("A1")
For iRow = 1 To iLastRow
If .Cells(iRow).value Like "##*" _
Then
.Cells(iRow).EntireRow.VerticalAlignment = xlTop
End If
Next iRow
End With
sStepID = "9. doing page layout"
With wsReport
With .PageSetup
.Orientation = xlLandscape
.Zoom = 90
.LeftMargin = Application.InchesToPoints(0.5)
.RightMargin = Application.InchesToPoints(0.5)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.CenterHorizontally = True
.CenterVertically = True
End With
With .Range("A21")
For iRow = 1 To iLastRow
If Trim(.Cells(iRow).value) = "" _
Then .Cells(iRow).RowHeight = 3
Next
.Cells(iLastRow + 1).RowHeight = 3
End With
.Columns("L:L").Copy
.Columns("M:M").PasteSpecial Paste:=xlPasteFormats
With .Columns("N:N")
.PasteSpecial Paste:=xlPasteFormats
With .Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
.Borders(xlInsideHorizontal).LineStyle = xlNone
.Borders(xlEdgeTop).LineStyle = xlNone
.Borders(xlEdgeBottom).LineStyle = xlNone
.ColumnWidth = 0.33
End With
sStepID = "10. formatting page bottom"
With .Range("A109:M109")
.RowHeight = 25
.MergeCells = True
.WrapText = True
.VerticalAlignment = xlTop
End With
With .Range("A114:M114")
.RowHeight = 25
.MergeCells = True
.WrapText = True
.VerticalAlignment = xlTop
End With
End With
sStepID = "11. formatting page top"
With wsReport
.Activate
.Range("A4:A19").EntireRow.Delete Shift:=xlUp
.Range("B3").Activate
.Range("C1").value = sPageHeaderText
With .Range("C1:K1")
.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlTop
.WrapText = False
.MergeCells = False
With .Font
.Name = "sans-serif"
.Size = 12
.Bold = True
End With
End With
.Range("M2").value = sRunLabel1
.Range("M3").value = sRunLabel2
.Range("M4").value = sRunLabel3
With .Range("M2:M4")
.HorizontalAlignment = xlRight
.WrapText = False
End With
End With
Application.CutCopyMode = False
sStepID = "12. adding line item numbers"
Call GetReportStationsData(avNumberRows())
With wsReport
.Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
.Columns("A:A").ColumnWidth = 2.43
For iStation = 1 To UBound(avNumberRows, 2)
With .Range("A1").Cells(avNumberRows(1, iStation))
For iRow = 1 To avNumberRows(2, iStation)
.Offset(iRow - 1) = iRow
.VerticalAlignment = xlTop
Next iRow
End With
Next iStation
End With
sStepID = "13. removing remaining merged cells"
With wsReport.Range("B1")
For iRow = 1 To iLastRow
With .Cells(iRow)
.MergeCells = False
.WrapText = False
End With
Next iRow
End With
sStepID = "14. adding has been processed name"
wsReport.Names.Add Name:="HasBeenFormatted", RefersTo:="=TRUE"
sStepID = "15. closing out"
Worksheets("Formulas").Activate
Application.EnableEvents = True
Closeout:
Exit Sub
ErrHandler:
Call ErrorMessage(Err.Number, Err.Description, sSubName, sStepID)
End Sub
Sub StationDataToFormulasSheet()
Dim avReportRanges() As Variant
Dim avFormulasAnchorsRows() As Variant
Dim wsFormulas As Worksheet
Dim wsReport As Worksheet
Dim iStation As Long
Dim iReportStationsCount As Long
Dim iFormulasStationsCount As Long
Dim iStationRowsCount As Long
Dim sStationName As String
Dim iHideRowsCount As Long
Dim rFormulasStationAnchor As Range
Dim rReportStationAnchor As Range
Dim iDataColumnsCount As Long
Dim iAllStationRowsCount As Long
Dim sSubName As String
Dim sStepID As String
sSubName = "StationDataToFormulasSheet"
sStepID = ""
On Error GoTo ErrHandler
iDataColumnsCount = 5
iAllStationRowsCount = 20
sStepID = "setting worksheet Report and Formulas objects"
Set wsFormulas = [Formulas]
Set wsReport = Worksheets("Report")
sStepID = "1. getting Report and Formulas worksheet rows' data"
Call GetReportStationsData(avReportRanges(), 1)
Call GetFormulasRowsData(avFormulasAnchorsRows())
iReportStationsCount = UBound(avReportRanges, 2)
iFormulasStationsCount = UBound(avFormulasAnchorsRows, 2)
sStepID = "2. processing Formulas worksheet data"
For iStation = 1 To iReportStationsCount
With wsFormulas
.Range("Station" & iStation & "Rows").EntireRow.Hidden = False
Set rFormulasStationAnchor = .Range("B1").Offset(avFormulasAnchorsRows(1, iStation))
rFormulasStationAnchor.Offset(-2).value = avReportRanges(3, iStation)
With rFormulasStationAnchor
.Resize(avFormulasAnchorsRows(2, iStation)).EntireRow.Hidden = True
.Resize(avFormulasAnchorsRows(2, iStation), iDataColumnsCount).value = ""
End With
With rFormulasStationAnchor
.Resize(avReportRanges(2, iStation)).EntireRow.Hidden = False
.Resize(avReportRanges(2, iStation)).EntireRow.AutoFit
End With
End With
sStepID = "3. processing Report worksheet data"
With wsReport
Set rReportStationAnchor = .Range("B1").Cells(avReportRanges(1, iStation))
With rReportStationAnchor.Resize(avReportRanges(2, iStation), iDataColumnsCount)
.Copy rFormulasStationAnchor
End With
rReportStationAnchor.Resize(avReportRanges(2, iStation)).EntireRow.AutoFit
End With
Next iStation
sStepID = "4. hiding unused Formulas station(s)"
If iReportStationsCount < iFormulasStationsCount _
Then
For iStation = iReportStationsCount + 1 To iFormulasStationsCount
[Formulas].Range("Station" & iStation & "Rows").EntireRow.Hidden = True
Next iStation
End If
sStepID = "5. formatting formula's cells' inner/vertical and right borders"
With wsFormulas
For iStation = 1 To iReportStationsCount
Set rFormulasStationAnchor = .Range("B1").Offset(avFormulasAnchorsRows(1, iStation))
With rFormulasStationAnchor.Resize(iAllStationRowsCount, iDataColumnsCount)
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.249946592608417
.Weight = xlThin
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.249946592608417
.Weight = xlThin
End With
End With
Next iStation
End With
Application.Calculate
Exit Sub
ErrHandler:
Call ErrorMessage(Err.Number, Err.Description, sSubName, sStepID)
End Sub