动态Excel Unpivot数据

时间:2016-10-12 12:08:50

标签: excel vba excel-vba unpivot excel-2016

我经常以预先交叉的格式获取数据,我想要将其取消。 我已经使用Excel 2016中的内置功能来生成大部分宏代码,但我需要使其动态化。

每周,列标题将是前几周的日期,并且宏已经硬编码记录marco时使用的特定日期集。

这是我的初始电子表格 Initial data

录制的宏提供以下内容:

    ActiveWorkbook.Queries.Add Name:="Table1", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.CurrentWorkbook(){[Name=""Table1""]}[Content]," & Chr(13) & "" & Chr(10) & _
    "    #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Code"", Int64.Type}, {""Product Description"", type text}, {""Store Number"", Int64.Type}, {""Store Name"", type text}, {""04/09/2016"", Int64.Type}, {""05/09/2016"", Int64.Type}, {""06/09/2016"", Int64.Type}, {""07/09/2016" & _
    """, Int64.Type}, {""08/09/2016"", Int64.Type}, {""09/09/2016"", Int64.Type}, {""10/09/2016"", Int64.Type}})," & Chr(13) & "" & Chr(10) & _
    "    #""Unpivoted Columns"" = Table.UnpivotOtherColumns(#""Changed Type"", {""Code"", ""Product Description"", ""Store Number"", ""Store Name""}, ""Attribute"", ""Value"")" & _
    Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Unpivoted Columns"""

我把它改成看起来像这样,但它没有用......

Dim col1 As String, col2 as String, col3 as String, col4 as String, col5 as String, col6 as String, col7 as String

col1 = Range("E1").Value
col2 = Range("F1").Value
col3 = Range("G1").Value
col4 = Range("H1").Value
col5 = Range("I1").Value
col6 = Range("J1").Value
col7 = Range("K1").Value

ActiveWorkbook.Queries.Add Name:="Table1", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.CurrentWorkbook(){[Name=""Table1""]}[Content]," & Chr(13) & "" & Chr(10) & _
    "    #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Code"", Int64.Type}, {""Product Description"", type text}, {""Store Number"", Int64.Type}, {""Store Name"", type text}, {" & col1 & ", Int64.Type}, {" & col2 & ", Int64.Type}, {" & col3 & ", Int64.Type}, {" & col4 & _
    """, Int64.Type}, {" & col5 & ", Int64.Type}, {" & col6 & ", Int64.Type}, {" & col7 & ", Int64.Type}})," & Chr(13) & "" & Chr(10) & _
    "    #""Unpivoted Columns"" = Table.UnpivotOtherColumns(#""Changed Type"", {""Code"", ""Product Description"", ""Store Number"", ""Store Name""}, ""Attribute"", ""Value"")" & _
    Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Unpivoted Columns"""

0 个答案:

没有答案