将行数据切换到列标题中

时间:2014-01-26 16:02:08

标签: excel vba ms-access

我有如下所示,一个excel工作表,其中数据按行排列,当前有3列具有逐年的年度数据,一列描述数据类型(营业额或销售退货等)和产品名称。

任何人都可以请教我如何将其加载到带有产品列,年份列和数据描述列的MS Access表中。或者,如果您可以推荐更好的方法来管理此数据。 前几年和未来几年将加载更多数据。

2010       2009       2008     Description   Product Name
---------  ---------  -------  ------------  -------------
1,937,190  1,719,600  816,633  Turnover      Product Name1
  791,788    599,117  743,114  Sales Return  Product Name1
   13,601  1,719,600  816,633  Turnover      Product Name2
  791,788    599,117  743,114  Sales Return  Product Name2

1 个答案:

答案 0 :(得分:1)

您可以在Access中创建将读取原始Excel数据的链接表。

Import or link to data in an Excel workbook

假设您的链接表名为[ExcelData]

2010     2009     2008    Description    Product Name 
-------  -------  ------  -------------  -------------
1937190  1719600  816633  Turnover       Product Name1
 791788   599117  743114  Sales Return   Product Name1
  13601  1719600  816633  Turnover       Product Name2
 791788   599117  743114  Sales Return   Product Name2

您可以在Access中创建以下名为[qryExcelDataUnpivoted]的已保存查询:

SELECT [Product Name], 2008 AS Yr, [Description], [2008] AS Amount
FROM ExcelData
UNION ALL
SELECT [Product Name], 2009 AS Yr, Description, [2009] AS Amount
FROM ExcelData
UNION ALL
SELECT [Product Name], 2010 AS Yr, Description, [2010] AS Amount
FROM ExcelData

返回

Product Name   Yr    Description    Amount 
-------------  ----  -------------  -------
Product Name1  2008  Turnover        816633
Product Name1  2008  Sales Return    743114
Product Name2  2008  Turnover        816633
Product Name2  2008  Sales Return    743114
Product Name1  2009  Turnover       1719600
Product Name1  2009  Sales Return    599117
Product Name2  2009  Turnover       1719600
Product Name2  2009  Sales Return    599117
Product Name1  2010  Turnover       1937190
Product Name1  2010  Sales Return    791788
Product Name2  2010  Turnover         13601
Product Name2  2010  Sales Return    791788

然后,如果您希望为每个[描述]

分别创建单独的列,则可以使用它来创建交叉表查询
TRANSFORM SUM(Amount) AS SumOfAmount
SELECT Yr, [Product Name]
FROM qryExcelDataUnpivoted
GROUP BY Yr, [Product Name]
PIVOT Description

返回

Yr    Product Name   Sales Return  Turnover
----  -------------  ------------  --------
2008  Product Name1        743114    816633
2008  Product Name2        743114    816633
2009  Product Name1        599117   1719600
2009  Product Name2        599117   1719600
2010  Product Name1        791788   1937190
2010  Product Name2        791788     13601