Sub MoveSplits()
'
' MoveSplits Macro
' This macro will delete Row 1 and Columns F:AE, search for each instance of the five desired splits,
' move each of those rows to Sheet2, sort the result by the employee's name, calculate AHT and ACW on the existing splits,
' adds 2 blank rows, add labels, freeze the top row and format all columns
'
Application.ScreenUpdating = False
'Delete Row 1
Rows("1:1").Select
Selection.Delete Shift:=xlUp
'Delete Columns F:AE
Columns("F:AE").Select
Selection.Delete Shift:=xlToLeft
'Select Columns A:E then alphabetizes them sorted by Column A (Rep Name column)
Columns("A:E").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1:A500") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:E500")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Format Columns D:E as a number with no decimal places
Columns("D:E").Select
Selection.NumberFormat = "0"
'Resize Columns A:G to fit the largest cell
Columns("A:G").Select
Columns("A:G").EntireColumn.AutoFit
'Find the last instance of each unique name in Column A and add a blank row beneath it
Dim lRow As Long
For lRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row To 2 Step -1
If Cells(lRow, "A") <> Cells(lRow - 1, "A") Then Rows(lRow).EntireRow.Insert
Next lRow
'Insert a row at the top for labels, add "AHT" to Cell F1 & "ACW" to Cell G1
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Rep Name"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Split"
Range("C1").Select
ActiveCell.FormulaR1C1 = "#Calls"
Range("D1").Select
ActiveCell.FormulaR1C1 = "AHT"
Range("E1").Select
ActiveCell.FormulaR1C1 = "ACW"
'Select Cells B1:G1 and center-aligns them as they're labels
Range("B1:G1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'Resize Columns A:E to fit the largest cell
Columns("A:E").Select
Columns("A:E").EntireColumn.AutoFit
'Freeze the top row
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
'Select Cell A1
Range("A1").Select
'Search for the string "CS MAIN 1", copies the entire row and pastes it into the first available row in Sheet2
Dim xRow&, NextRow&, LastRow&
NextRow = 1
LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For xRow = 1 To LastRow
If WorksheetFunction.CountIf(Rows(xRow), "CS MAIN 1") > 0 Then
Rows(xRow).Copy Sheets("Sheet2").Rows(NextRow)
NextRow = NextRow + 1
End If
Next xRow
'Search for the string "CS GCS 12", copies the entire row and pastes it into the first available row in Sheet2
NextRow = Sheets("Sheet2").Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For xRow = 1 To LastRow
If WorksheetFunction.CountIf(Rows(xRow), "CS GCS 12") > 0 Then
Rows(xRow).Copy Sheets("Sheet2").Rows(NextRow)
NextRow = NextRow + 1
End If
Next xRow
'Search for the string "CS REFILLS 33", copies the entire row and pastes it into the first available row in Sheet2
NextRow = Sheets("Sheet2").Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For xRow = 1 To LastRow
If WorksheetFunction.CountIf(Rows(xRow), "CS REFILLS 33") > 0 Then
Rows(xRow).Copy Sheets("Sheet2").Rows(NextRow)
NextRow = NextRow + 1
End If
Next xRow
'Search for the string "Retail 74", copies the entire row and pastes it into the first available row in Sheet2
NextRow = Sheets("Sheet2").Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For xRow = 1 To LastRow
If WorksheetFunction.CountIf(Rows(xRow), "Retail 74") > 0 Then
Rows(xRow).Copy Sheets("Sheet2").Rows(NextRow)
NextRow = NextRow + 1
End If
Next xRow
'Search for the string "Diabetic Meter 79", copies the entire row and pastes it into the first available row in Sheet2
NextRow = Sheets("Sheet2").Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For xRow = 1 To LastRow
If WorksheetFunction.CountIf(Rows(xRow), "Diabetic Meter 79") > 0 Then
Rows(xRow).Copy Sheets("Sheet2").Rows(NextRow)
NextRow = NextRow + 1
End If
Next xRow
'Rename sheet
' If Environ("USERNAME") = "SAPOLZ" Then
' ActiveSheet.Name = "Team Paradise as of " & Format(Now() - 1, "mm-dd-yyyy")
' ElseIf Environ("USERNAME") = "C34317" Then
' ActiveSheet.Name = "Team Ganje as of " & Format(Now() - 1, "mm-dd-yyyy")
' ElseIf Environ("USERNAME") = "TKSEVE" Then
' ActiveSheet.Name = "Team Severson as of " & Format(Now() - 1, "mm-dd-yyyy")
' ElseIf Environ("USERNAME") = "C40757" Then
' ActiveSheet.Name = "Team Gochal as of " & Format(Now() - 1, "mm-dd-yyyy")
' ElseIf Environ("USERNAME") = "DLDARD" Then
' ActiveSheet.Name = "Team Dardis as of " & Format(Now() - 1, "mm-dd-yyyy")
' ElseIf Environ("USERNAME") = "LJHAAG" Then
' ActiveSheet.Name = "Team Gartamaker as of " & Format(Now() - 1, "mm-dd-yyyy")
' ElseIf Environ("USERNAME") = "C35119" Then
' ActiveSheet.Name = "Team Kathman as of " & Format(Now() - 1, "mm-dd-yyyy")
' Else
' End If
'Select Sheet2 to make it the active sheet
Sheets("Sheet2").Select
'Select Columns A:E then alphabetize them sorted by Column A (Rep Name column)
Columns("A:E").Select
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("A1:A500") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet2").Sort
.SetRange Range("A1:E500")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Enter the following formula in Cell F1 and extend it to Cell F500: "If(A1="","",D1*C1)"
Range("F1").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-5]="""","""",RC[-2]*RC[-3])"
Selection.AutoFill Destination:=Range("F1:F500"), Type:=xlFillDefault
Range("F1:F500").Select
'Enter the following formula in Cell G1 and extend it to Cell F500: "If(A1="","",E1*C1)"
Range("G1").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-6]="""","""",RC[-2]*RC[-4])"
Selection.AutoFill Destination:=Range("G1:G500"), Type:=xlFillDefault
Range("G1:G500").Select
'Format Columns D:G as a number with no decimal places
Columns("D:G").Select
Selection.NumberFormat = "0"
'Find the last instance of each unique name in Column A and adds 2 blank rows beneath it
Const blanks = 2
Dim lastValue As String, i As Long, r As Long
Do
r = r + 1
If r > 1 And lastValue <> Cells(r, 1).Value Then
If Cells(r, 1).Value = "" Then Exit Do
For i = 1 To blanks
Rows(r).Insert Shift:=xlDown
Next
r = r + blanks
End If
lastValue = Cells(r, 1).Value
Loop
'Select Columns C:G and right-align them as they're numbers
Columns("C:G").Select
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'Insert two rows at the top for labels and an additional blank row, add labels to each Column
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Rep Name"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Split"
Range("C1").Select
ActiveCell.FormulaR1C1 = "#Calls"
Range("D1").Select
ActiveCell.FormulaR1C1 = "AHT"
Range("E1").Select
ActiveCell.FormulaR1C1 = "ACW"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Combined AHT"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Combined ACW"
'Select Cells B1:G1 and center-align them as they're labels
Range("B1:G1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'Resize Columns A:G to fit the largest cell
Columns("A:G").Select
Columns("A:G").EntireColumn.AutoFit
'Freeze the top row
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
'Find and go to Column C in the first blank row
Application.Goto Range("A3").End(xlDown).Offset(1, 2)
'Rename Sheet
'ActiveSheet.Name = "Combined Stats Calculations"
Application.ScreenUpdating = True
End Sub
Bookmarks