Sub test()
Dim lr&, cell As Range, i&, x&, y&, a$, b$, c$, d$, e$, f$, g$, h$, ws As Worksheet
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error GoTo earlyexit
If ActiveSheet.Name <> "Full Report" Then Exit Sub
a = ActiveSheet.Range("A2").Value
b = ActiveSheet.Range("B2").Value
c = ActiveSheet.Range("C2").Value
d = ActiveSheet.Range("D2").Value
e = ActiveSheet.Range("E2").Value
f = ActiveSheet.Range("F2").Value
g = ActiveSheet.Range("G2").Value
h = ActiveSheet.Range("H2").Value
lr = Cells(Rows.Count, 1).End(xlUp).Row
Columns(1).Insert
Range("A1").FormulaR1C1 = "=IFERROR(RIGHT(RC[1], LEN(RC[1])-3),"""")"
Range("A1").AutoFill Destination:=Range("A1:A" & lr), Type:=xlFillDefault
Columns(1).Copy
Columns(1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Range("A1", ActiveCell.SpecialCells(xlLastCell)).Select
Worksheets("Full Report").Sort.SortFields.Clear
Worksheets("Full Report").Sort.SortFields.Add Key:=Range("A3:A" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
With Worksheets("Full Report").Sort
.SetRange Range("A2:H" & lr)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For i = lr To 1 Step -1
If Cells(i, 1).Value = "" Or Len(Cells(i, 1)) < 4 Or IsNumeric(Mid(Cells(i, 1), 4, 1)) = False Then Cells(i, 1).EntireRow.Delete
Next i
Columns(1).Delete
Rows(1).Insert
Range("A1").Value = "iloveexcelforum.com"
lr = Cells(Rows.Count, 1).End(xlUp).Row
For Each cell In Range("A2:A" & lr)
i = InStr(cell.Value, "-")
If Mid(cell.Value, i, Len(cell.Value) - i) <> Mid(cell.Offset(-1, 0).Value, i, Len(cell.Offset(-1, 0).Value) - i) Then
If cell.Row = 2 Then GoTo nc
On Error Resume Next
Set ws = Worksheets(Mid(cell.Offset(-1, 0).Value, i + 1, Len(cell.Offset(-1, 0).Value) - i))
On Error GoTo earlyexit
If ws Is Nothing Then Worksheets.Add(After:=ActiveSheet).Name = Mid(cell.Offset(-1, 0).Value, i + 1, Len(cell.Offset(-1, 0).Value) - i)
Range("A" & x & ":A" & cell.Row - 1).EntireRow.Copy
Worksheets(Mid(cell.Offset(-1, 0).Value, i + 1, Len(cell.Offset(-1, 0).Value) - i)).Range("A1").PasteSpecial
Application.CutCopyMode = False
nc:
x = cell.Row
End If
Next cell
For i = 1 To Worksheets.Count
If Worksheets(i).Range("A1").Value = "" Then GoTo nxi
If Worksheets(i).Name <> "Full Report" Then
Worksheets(i).Rows(1).Insert
Worksheets(i).Rows(1).Insert
Worksheets(i).Rows(1).Insert
Worksheets(i).Range("A1").Value = Worksheets(i).Name
Worksheets(i).Range("A2").Value = a
Worksheets(i).Range("B2").Value = b
Worksheets(i).Range("C2").Value = c
Worksheets(i).Range("D2").Value = d
Worksheets(i).Range("E2").Value = e
Worksheets(i).Range("F2").Value = f
Worksheets(i).Range("G2").Value = g
Worksheets(i).Range("H2").Value = h
Worksheets(i).Range("A3").Value = "CUP " & Left(Worksheets(i).Range("A4"), InStr(Worksheets(i).Range("A4"), "-") - 1)
Worksheets(i).Rows("1:3").Font.Bold = True
lr = Worksheets(i).Cells(Rows.Count, 1).End(xlUp).Row
y = 4
x = 5
Do Until x = lr + 60
If Left(Worksheets(i).Cells(x, 1), InStr(Worksheets(i).Cells(x, 1), "-")) <> Left(Worksheets(i).Cells(x - 1, 1), InStr(Worksheets(i).Cells(x - 1, 1), "-")) Then
Worksheets(i).Rows(x).Insert
Worksheets(i).Rows(x).Insert
Worksheets(i).Rows(x).Insert
Worksheets(i).Rows(x).Insert
Worksheets(i).Cells(x + 1, 1).Value = "Cup " & Left(Worksheets(i).Cells(x - 1, 1), InStr(Worksheets(i).Cells(x - 1, 1), "-")) & " Total :"
Worksheets(i).Cells(x + 1, 5).FormulaR1C1 = "=SUM(R[-" & x - y + 1 & "]C:R[-2]C)"
Worksheets(i).Cells(x + 1, 6).FormulaR1C1 = "=SUM(R[-" & x - y + 1 & "]C:R[-2]C)"
Worksheets(i).Cells(x + 1, 7).FormulaR1C1 = "=SUM(R[-" & x - y + 1 & "]C:R[-2]C)"
Worksheets(i).Cells(x + 3, 1).Value = "Cup " & Left(Worksheets(i).Cells(x + 4, 1), InStr(Worksheets(i).Cells(x + 4, 1), "-"))
Worksheets(i).Rows(x & ":" & x + 3).Font.Bold = True
x = x + 4
y = x
End If
x = x + 1
Loop
End If
nxi:
Next i
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
earlyexit:
Application.ScreenUpdating = True
Application.EnableEvents = True
MsgBox Err.Description
End Sub
Please remember to click the
Bookmarks