Transposing columns

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
217
Office Version
  1. 2016
Platform
  1. Windows
Hello experts,

I am trying to create a pivot table from an unpivot table (pics) below:


Pivot table:
PivotTable.jpg


Unpivot table:
UnpivotTable.jpg


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?

Debug.png
 
Thanks for you Patience
I went about it a different way
I opened the new table you created and from i created a single record per each dated Columns using the static fields to fill in the other information


VBA Code:
Option Compare Database

Sub DoSomething()
Dim db As DAO.Database
Dim UnPivot As DAO.Recordset
Dim Pivot 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"
    End If
Next tbl

db.Execute "CREATE TABLE T_Pivot(Stores CHAR, Cities CHAR, Period DATETIME, SalesQty INTEGER);"


Set Pivot = db.OpenRecordset("T_Pivot", dbOpenTable)
Set UnPivot = db.OpenRecordset("T_Unpivot", dbOpenTable)

Dim Fld As Integer
Dim str As String

 Do
    With Pivot
        For Fld = 2 To UnPivot.Fields.Count - 1
            .AddNew
            .Fields("Cities").Value = UnPivot.Fields("Cities").Value
            .Fields("Stores").Value = UnPivot.Fields("Stores").Value
            .Fields("Period").Value = DateValue(UnPivot.Fields(Fld).Name)
            .Fields("SalesQty").Value = UnPivot.Fields(Fld).Value
            .Update
        Next Fld
       
    End With
    UnPivot.MoveNext
 Loop Until UnPivot.EOF
 
 Pivot.Close
 UnPivot.Close
 Set Pivot = Nothing
 Set UnPivot = Nothing
 Set db = Nothing
 Set tbl = Nothing
End Sub
THANKS a lot @EFANYoutube!! There is just one issue, when I import the data, Access automatically creates a new column F10 into my Unpivot table (as last col), so when I run the code I get the error:
Run-time error '13' -- Type mismatch. the sub is stopping at that point.

1624412836625.png


If I delete that column F10 I get the error below.

Run-time error '2391'
Field 'F10' doesn't exist in destination table 'T_Unpivot.'
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You can bring the data directly into PQ from Access. No need to bring it into Excel. Once you unpivot the data move it directly to Power Pivot and do your Pivot Table there.
 
Upvote 0
THANKS a lot @EFANYoutube!! There is just one issue, when I import the data, Access automatically creates a new column F10 into my Unpivot table (as last col), so when I run the code I get the error:
Run-time error '13' -- Type mismatch. the sub is stopping at that point.

View attachment 41403

If I delete that column F10 I get the error below.

Run-time error '2391'
Field 'F10' doesn't exist in destination table 'T_Unpivot.'
Ok
If its always "F10" then add the following (Don't delete F10)
VBA Code:
        For Fld = 2 To UnPivot.Fields.Count - 1
           If UnPivot.Fields(Fld).Name <> "F10" Then
            .AddNew
            .Fields("Cities").Value = UnPivot.Fields("Cities").Value
            .Fields("Stores").Value = UnPivot.Fields("Stores").Value
            .Fields("Period").Value = DateValue(UnPivot.Fields(Fld).Name)
            .Fields("SalesQty").Value = UnPivot.Fields(Fld).Value
            .Update
           End If
        Next Fld
 
Last edited:
Upvote 0
Solution
Ok
If its always "F10" then add the following (Don't delete F10)
VBA Code:
        For Fld = 2 To UnPivot.Fields.Count - 1
           If UnPivot.Fields(Fld).Name <> "F10" Then
            .AddNew
            .Fields("Cities").Value = UnPivot.Fields("Cities").Value
            .Fields("Stores").Value = UnPivot.Fields("Stores").Value
            .Fields("Period").Value = DateValue(UnPivot.Fields(Fld).Name)
            .Fields("SalesQty").Value = UnPivot.Fields(Fld).Value
            .Update
           End If
        Next Fld
Thanks @EFANYoutube! It works perfectly now.
 
Upvote 0

Forum statistics

Threads
1,214,586
Messages
6,120,402
Members
448,958
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top