Guinaba
Board Regular
- Joined
- Sep 19, 2018
- Messages
- 233
- Office Version
- 2016
- Platform
- Windows
Hello experts,
I am trying to create a pivot table from an unpivot table (pics) below:
Pivot table:
Unpivot table:
Using the following code:
However, not sure I am not able to recognize the fields Period and SalesQty, any suggestion?
I am trying to create a pivot table from an unpivot table (pics) below:
Pivot table:
Unpivot table:
Using the following code:
VBA Code:
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim tbl As TableDef
Set db = CurrentDb
For Each tbl In db.TableDefs
If tbl.Name = "T_Pivot" Then
db.Execute "DROP TABLE T_Pivot"
Else
End If
Next tbl
db.Execute "CREATE TABLE T_Pivot(Stores CHAR, Cities CHAR, Period DATETIME, SalesQty INTEGER);"
Set db = CurrentDb
Set rst = db.OpenRecordset("T_Unpivot", dbOpenTable)
Dim n As Integer
Dim fieldname As String
Dim str As String
Do
For n = 1 To rst.Fields.Count - 1
fieldname = rst.Fields(n).Name
str = "Insert Into T_Pivot (Stores,Cities,Period,SalesQty) " _
& "VALUES (" & rst![Stores] & ", " & rst![Cities] & ", " & rst![Period] & "," & rst![SalesQty] & ")"
Debug.Print str
Next n
'rst.MoveNext
db.Execute str
Loop Until rst.EOF
End Sub
However, not sure I am not able to recognize the fields Period and SalesQty, any suggestion?