Sub ProcessSAPOutput()
'Define variables myRange and myCell as ranges.
Dim myRange As Range
Dim cRange As Range
Dim dRange As Range
Dim myCell As Range
Dim mySelection As Range
'Delete sheet2 and sheet3. Create the SAP Raw Data and SAP Refined Data sheets, and remove grouping on the latter.
'Autofit all columns.
Sheets(Array("Sheet2", "Sheet3")).Select
ActiveWindow.SelectedSheets.Delete
Sheets("Sheet1").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = "SAP Raw Data"
ActiveSheet.Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = "SAP Refined Data"
Sheets("SAP Refined Data").Select
Cells.Select
Selection.Rows.Ungroup
Selection.Rows.Ungroup
Selection.Font.Name = "Calibri"
Selection.Columns.AutoFit
'Delete columns G and H, then delete all rows with blank values in column A.
Range("G:H").Delete
Range("A:A").Select
Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
'Name/Define row 1 on SAP Refined Data sheet as "myRange" and format myRange. Also define the cRange, myCell and
'myField ranges.
Set myRange = Sheets("SAP Refined Data").Range("1:1")
Set cRange = myRange.Find(What:="Cost Element").Offset(1, 0)
Set cRange = Range(cRange, cRange.End(xlDown))
Set myCell = myRange.Find(What:="Dr/Cr indicator")
Set dRange = myCell.Offset(1, 0)
Set dRange = Range(dRange, dRange.End(xlDown))
Set eRange = myRange.Find(What:="Period").Offset(1, 0)
Set SourceData = Sheets("SAP Refined Data").UsedRange
Set mySelection = Range(ActiveCell, ActiveCell.End(xlDown))
myField = myCell.Column
myRange.Select
Selection.Font.Bold = True
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
'Convert values in column D to a number format.
Range("A65536").Value = "1"
Range("A65536").Copy
cRange.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlMultiply
Rows("65536:65536").Delete
'Rename the header in cell B1 to "Segment".
Range("B1").Value = "Segment"
'Delete the settlement rows.
myRange.AutoFilter
myCell.Select
Selection.AutoFilter Field:=myField, Criteria1:="O"
dRange.Rows.Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Delete Shift:=xlUp
myRange.AutoFilter
dRange.Select
myRange.Select
Selection.Find(What:="Segment").Select
ActiveCell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Formula = "=IF(S2=""D.02642"",""Legacy"",IF(S2=""M.00453"",""SMALL COMMERCIAL"",""RESIDENTIAL""))"
ActiveCell.EntireColumn.Select
Selection.Columns.AutoFit
myRange.Select
Selection.Find(What:="CO Area Currency").Select
Selection.Value = "Cost Category"
ActiveCell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Formula = "=IF(OR(LEFT(C2,11)=""D.02642.1.5"",MID(C2,9,1)=""3""),""INCENTIVES"",IF(LEFT(C2,7)=""D.02642"",""ADMINISTRATIVE COST"",IF(MID(C2,9,1)=""2"",""ADMINISTRATIVE COST"",IF(MID(C2,9,1)=""1"",""CAPITAL""))))"
Selection.Columns.AutoFit
eRange.Select
mySelection.Select
End Sub