Excel如何转换用于透视的表:使用字段中的列标题
我有一个Excel表格,填写如下Excel如何转换用于透视的表:使用字段中的列标题,excel,vba,data-modeling,excel-tables,Excel,Vba,Data Modeling,Excel Tables,我有一个Excel表格,填写如下 PROD JAN-19 FEB-19 ... ... product1 123 098 ... ... product2 314 467 ... ... 我需要使用数据透视表中的数据,所以我想我应该使用MMM-YY头作为年份和月份字段中的数据,按照以下方式转换数据模型 PROD Year Month Da
PROD JAN-19 FEB-19 ... ...
product1 123 098 ... ...
product2 314 467 ... ...
我需要使用数据透视表中的数据,所以我想我应该使用MMM-YY头作为年份和月份字段中的数据,按照以下方式转换数据模型
PROD Year Month Data
Product1 19 JAN 123
Product1 19 FEB 098
... ... ... ...
下面是实际表格的屏幕以及我希望实现的目标:
有没有一种切实可行的方法来实现这一点,或者是在上传表格的数据模型上工作,或者是作为最后一个选项VBA?通过
Application.Index()
Function getRowsArr(ByVal ItemsCount, Optional ByVal n& = 12) As Variant()
' Purpose: return 1-dim 0-based array containing n row numbers per item + title row no 1
' Note: allows restructuring the original array to contain all months data
Const START& = 2 ' data rang starts in 2nd row
Dim tmp(), i&, ii&
ReDim tmp(0 To (ItemsCount - 1) * n) ' includes header row number 1 at tmp(0) - zerobound!
'1) fill temporary array
tmp(0) = 1 ' title row no equals 1
For i = START To ItemsCount ' row no 2 to ...
For ii = 0 To n - 1 ' repeat row number n times
tmp((i - START) * n + ii + 1) = i
Next ii
Next i
'2) return Array(1,2,2,2,2,2,2,2,2,2,2,2,2,3,........3,4...,...) as function value
getRowsArr = tmp
'Debug.Print Left$(Join(tmp, ","), 65) & "..."
End Function
这种方法首先将表值分配给数组,并最终使用Application.Index()
函数的扩展功能将同一数组转换为所需的结构,从而维护产品名称
我并不认为这是最有效的方法,但它清楚地证明了所引用函数-c.f的可能性
进一步注意:我假设包含所有月份数据,即即使是空值
Sub Table2PivotBase()
' Purpose: Transform Table to Pivot base
' Method: reorganize Datafield array of table using advanced features of Application.Index in [2]d)
' Note: sheet references use the worksheet's CodeName property here
'~~~~~~~~~~~~ [0] Reference to table (address) ~~~~~~
Const MONTHSCOUNT& = 12, COLUMNOFFSET& = 1 ' number of months, column offset January = 1 (i.e. 2nd col)
With Sheet1
'a) Refer to table using the sheet's CodeName
Dim rpt As ListObject
Set rpt = .ListObjects("Table1")
Sheet1.CodeName
' ~~~~~~~~~~~ [1] Get Data ~~~~~~~~~~~~~~~~~~~~~~~~
'b) Assign table values in Sheet1 to 2-dim 1-based array
Dim arr As Variant, yr&
arr = .Range(rpt.Range.Address).Value2
'c) Extract current year from 1st month column in header (1+ coloffset 1 => 2nd column)
yr = Val(Split(arr(1, 1 + COLUMNOFFSET) & "-", "-")(1))
End With
'~~~~~~~~~~~~~~ [2] Reorganize Data ~~~~~~~~~~~~~~~
'd) Redimension array preserving 1st column
Dim arr2, ItemsCount&
ItemsCount = UBound(arr) - IIf(rpt.ShowTotals, 1, 0) ' exclude table totals from items count
arr2 = Application.Index(arr, Application.Transpose(getRowsArr(ItemsCount, MONTHSCOUNT)), Array(1, 1, 1, 2))
'e) Redefine headers in 1st row
Dim no&, headers
headers = Array("Product", "Year", "Month", "Data")
For no = 1 To 4
arr2(1, no) = headers(no - 1) ' headers are zerobased
Next no
'f) Enter year, month & month data in a loop
Const START& = 2
Dim i&, mon&, ii&
ii = START - 1
For i = START To UBound(arr2)
mon = (i - START) Mod MONTHSCOUNT + 1 ' 0 to 11 (omitting 1 caption row) + 1
If mon = 1 Then ii = ii + 1 ' increment data row of arr1 in January
'arr2(i, 1) has already been prefilled by section [2]d)
arr2(i, 2) = yr
arr2(i, 3) = Application.Text(DateSerial(yr, mon, 1), "mmm")
arr2(i, 4) = arr(ii, mon + COLUMNOFFSET)
Next i
'~~~~~~~~~~~~ ~[3] Write back pivot base to any sheet (via CodeName) ~~~~~~~~~~~
With Sheet2
.Range("A:D") = vbNullString
.Range("A1").Resize(UBound(arr2), UBound(arr2, 2)) = arr2
End With
End Sub
辅助函数getrowsar()
Function getRowsArr(ByVal ItemsCount, Optional ByVal n& = 12) As Variant()
' Purpose: return 1-dim 0-based array containing n row numbers per item + title row no 1
' Note: allows restructuring the original array to contain all months data
Const START& = 2 ' data rang starts in 2nd row
Dim tmp(), i&, ii&
ReDim tmp(0 To (ItemsCount - 1) * n) ' includes header row number 1 at tmp(0) - zerobound!
'1) fill temporary array
tmp(0) = 1 ' title row no equals 1
For i = START To ItemsCount ' row no 2 to ...
For ii = 0 To n - 1 ' repeat row number n times
tmp((i - START) * n + ii + 1) = i
Next ii
Next i
'2) return Array(1,2,2,2,2,2,2,2,2,2,2,2,2,3,........3,4...,...) as function value
getRowsArr = tmp
'Debug.Print Left$(Join(tmp, ","), 65) & "..."
End Function
请允许我问一个问题,您是否可以尝试我的解决方案来创建数据透视表的数据库-如果有帮助,请随意标记我的方法为正确。答案旁边的绿色复选标记表示接受-cf。做一次(巡回)[.标记答案将帮助其他开发人员不去讨论这个问题,因为它已经解决了,并专注于其他问题。谢谢!