Option Explicit
Sub Delete_Columns_G_to_Q()
Range("G:Q").Delete
ActiveWorkbook.Save
End Sub
Sub Main_without_Sort()
'uncomment if you want to write sample data
'Call SampleData
CreateNumbers
CopyResults
CreatePivot
End Sub
Sub Main_including_Sort()
'uncomment if you want to write sample data
'Call SampleData
SortEverySingleRow_by_Column
CreateNumbers
CopyResults
CreatePivot
End Sub
Sub SampleData()
Dim a(10) As String
Dim b() As String
Dim numParts As Integer
Dim iCt As Integer
Dim jCt As Integer
a(1) = "23 34 56 02 10"
a(2) = "10 52 34 23 02"
a(3) = "56 02 10 22 33"
a(4) = "42 05 08 76 51"
a(5) = "23 56 02 10 15"
a(6) = "88 86 56 10 03"
With Range("A:F")
.HorizontalAlignment = xlCenter
End With
For iCt = 1 To 6
b = Split(a(iCt), " ")
numParts = UBound(b) + 1
Range(Cells(iCt, 1), Cells(iCt, numParts)).Value = b
For jCt = 1 To 5
Cells(iCt, jCt).Value = Cells(iCt, jCt).Value
Debug.Print Cells(iCt, jCt).Address
Next jCt
Next iCt
End Sub
Sub SortEverySingleRow_by_Column()
Dim iCt As Integer
Dim sortRange As Range
For iCt = 1 To 6
Set sortRange = Range("A1:E1")
If iCt > 1 Then
Set sortRange = Range("A1:E1").Offset(iCt - 1, 0)
End If
'Debug.Print sortRange.Address
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=sortRange, _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange sortRange
.Header = xlGuess
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
Next iCt
End Sub
Sub CreateNumbers()
Dim iCt As Integer
Dim jCt As Integer
With Columns("G:M")
.ColumnWidth = 13
.HorizontalAlignment = xlCenter
End With
For iCt = 0 To 5
Range("G1").Offset(iCt, 0).Select
Call CreateFormulas
Next iCt
End Sub
Sub CreateFormulas()
ActiveCell.FormulaR1C1 = _
"=TEXT(RC[-6],""00"")& "" "" & TEXT(RC[-5],""00"")& "" "" & TEXT(RC[-4],""00"")& "" "" & TEXT(RC[-3],""00"")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = _
"=TEXT(RC[-7],""00"")& "" "" & TEXT(RC[-6],""00"")& "" "" & TEXT(RC[-5],""00"")& "" "" & TEXT(RC[-3],""00"")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = _
"=TEXT(RC[-8],""00"")& "" "" & TEXT(RC[-7],""00"")& "" "" & TEXT(RC[-5],""00"")& "" "" & TEXT(RC[-4],""00"")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = _
"=TEXT(RC[-9],""00"")& "" "" & TEXT(RC[-7],""00"")& "" "" & TEXT(RC[-6],""00"")& "" "" & TEXT(RC[-5],""00"")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = _
"=TEXT(RC[-9],""00"")& "" "" & TEXT(RC[-8],""00"")& "" "" & TEXT(RC[-7],""00"")& "" "" & TEXT(RC[-6],""00"")"
End Sub
Sub CopyResults()
Dim lastRow As Long
Dim colCt As Integer
lastRow = Range("G1").SpecialCells(xlCellTypeLastCell).Row
'Debug.Print lastRow
Range("M1").Value = "RESULTS"
For colCt = 1 To 5
Range("F1:F" & lastRow).Offset(0, colCt).Copy
'Debug.Print Range("F1:F" & lastRow).Offset(0, colCt).Address
Range("M2").Offset(lastRow * (colCt - 1), 0).PasteSpecial xlPasteValues
'Range("M2").Offset(lastRow * (colCt - 1), 1).Value = "colCt = " & colCt
Application.CutCopyMode = False
Next colCt
Range("N1").Select
End Sub
Sub CreatePivot()
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Range("M1").CurrentRegion, Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="Sheet1!R1C15", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion15
Sheets("Sheet1").Select
Cells(1, 15).Select
Range("P5").Select
With ActiveSheet.PivotTables("PivotTable1")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("RESULTS")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("RESULTS"), "Sum of RESULTS", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of RESULTS")
.Caption = "Count of RESULTS"
.Function = xlCount
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("RESULTS").AutoSort _
xlDescending, "Count of RESULTS", ActiveSheet.PivotTables("PivotTable1"). _
PivotColumnAxis.PivotLines(1), 1
Range("G1").Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("RESULTS")
.Orientation = xlRowField
.Position = 1
End With
ActiveWorkbook.ShowPivotTableFieldList = False
End Sub