Title Order is not expected, when Access Pivot exported to Excel File.

时间:2018-07-25 05:21:03

标签: excel vba ms-access export pivot

I export Access pivots to Excel files, and some of them are having not correct column order -- I'd like to have the total column is sure to be at the end of columns as follos;

Expected:

StaffCode, Name, 2018/04, 2018/05, 2018/06, TOTAL

Result:

StaffCode, Name, 2018/04, TOTAL, 2018/06, 2018/05 
<-- TOTAL column is coming before/in pivot columns unexpectedly (some pivot comes as expected)

Usually, I convert pivot query to table and then copy recordset to new excel file. If you've overcame this kind of issue, please let me know about your solutions.

pivot query:

TRANSFORM 
    CDbl(Nz(SUM(TotalHours),0)) AS TotalHours
SELECT
    StaffCode,
    StaffName,
    SUM(TotalHours) AS TOTAL
FROM
    SRC_TABLE
GROUP BY
    StaffCode,
    StaffName
ORDER BY
    StaffCode
PIVOT 
   REG_YM;

convert query to table:

DoCmd.TransferDatabase acExport, "Microsoft Access", Application.CurrentDb.Name, acTable, SrcQueryName, ConvTblName, False

export to excel:

Set rst = DB.OpenRecordset(TableName, dbOpenTable)
Set Ws = objExcel.Worksheets("XXXX")
 Ws.Cells(2, 1).CopyFromRecordset rst

Any advice would be appreciated. Thank you.

0 个答案:

没有答案