See if the sheet gives you the result you want.
I made this with several macro's.
Run this macro Sub CONVERTROWSTOCOL_Oeldere_revisted()
Sub CONVERTROWSTOCOL_Oeldere_revisted()
Dim rsht1 As Long, rsht2 As Long, i As Long, col As Long, wsTest As Worksheet
'check if sheet "ouput" already exist
Const strSheetName As String = "Output"
Set wsTest = Nothing
On Error Resume Next
Set wsTest = ActiveWorkbook.Worksheets(strSheetName)
On Error GoTo 0
If wsTest Is Nothing Then
Worksheets.Add.Name = strSheetName
End If
With Sheets("Output")
.UsedRange.ClearContents
.Range("A1:E1").Value = Array("date", "Exporter", "Importer", "Product", "Value")
End With
rsht1 = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row
rsht2 = Sheets("Output").Range("A" & Rows.Count).End(xlUp).Row
col = 4
For i = 2 To rsht1
Do While Sheets("sheet1").Cells(2, col).Value <> "" 'And Sheets("sheet1").Cells(i, col).Value <> ""
rsht2 = rsht2 + 1
Sheets("Output").Range("A" & rsht2).Value = Sheets("sheet1").Range("A" & i).Value
Sheets("Output").Range("B" & rsht2).Value = Sheets("sheet1").Range("B" & i).Value
Sheets("Output").Range("C" & rsht2).Value = Sheets("sheet1").Range("C" & i).Value
Sheets("Output").Range("D" & rsht2).Value = Sheets("sheet1").Cells(1, col).Value
Sheets("Output").Range("E" & rsht2).Value = Sheets("sheet1").Cells(i, col).Value
col = col + 1
Loop
col = 4
Next
With Sheets("Output")
Call Add_data_to_the_table
Columns("G:H").NumberFormat = "00"
Columns("A:Z").EntireColumn.AutoFit
Call Make_Pivot_Table_recorded
End With
End Sub
Sub Add_data_to_the_table()
Range("B2").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$H$250"), , xlYes).Name = _
"Tabel1"
Range("F1").FormulaR1C1 = "year"
Range("G1").FormulaR1C1 = "month"
Range("H1").FormulaR1C1 = "day"
Range("F2").FormulaR1C1 = "=IF(RC[-5]="""","""",YEAR(RC[-5]))"
Range("G2").FormulaR1C1 = "=IF(RC[-6]="""","""",MONTH(RC[-6]))"
Range("H2").FormulaR1C1 = "=IF(RC[-7]="""","""",DAY(RC[-7]))"
End Sub
Sub Make_Pivot_Table_recorded()
Const strSheetName As String = "PivotTable"
Set wsTest = Nothing
On Error Resume Next
Set wsTest = ActiveWorkbook.Worksheets(strSheetName)
On Error GoTo 0
If wsTest Is Nothing Then
Worksheets.Add.Name = strSheetName
End If
With Sheets("PivotTable")
.UsedRange.ClearContents
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Tabel1", Version:=xlPivotTableVersion15).CreatePivotTable TableDestination _
:="PivotTable!R3C1", TableName:="Draaitabel1", DefaultVersion:= _
xlPivotTableVersion15
Sheets("PivotTable").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("Draaitabel1").PivotFields("year")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("Draaitabel1").PivotFields("month")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("Draaitabel1").PivotFields("Exporter")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("Draaitabel1").PivotFields("Importer")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("Draaitabel1").AddDataField ActiveSheet.PivotTables( _
"Draaitabel1").PivotFields("Value"), "Aantal van Value", xlCount
With ActiveSheet.PivotTables("Draaitabel1").PivotFields("Product")
.Orientation = xlColumnField
.Position = 1
End With
ActiveWindow.SmallScroll Down:=3
With ActiveSheet.PivotTables("Draaitabel1").PivotFields("Product")
.PivotItems("(blank)").Visible = False
End With
Range("H14").Select
End With
End Sub
Bookmarks