当源数据包含日期的列标题时,如何构建合并的数据透视表?

发布于 2024-08-04 18:18:43 字数 636 浏览 3 评论 0原文

我有一位客户目前正在使用 Excel 进行员工规划。他们有许多针对不同项目的工作簿,每个项目包含 1 张或多张包含实际人员配置数据的工作表:

员工计划表样本

客户希望将所有这些工作表和工作簿中的所有数据合并到一个数据透视表中。 “合并”数据透视表不是一个选项,因为他们希望能够混淆源数据中的所有(非日期)字段。他们不想仅限于“行”和“列”。我当前的解决方案是一个宏,它通过相当复杂的复制和旋转过程来合并工作簿中的所有数据。我首先复制一行“元数据”(不是日期的所有内容),然后将元数据行的日期复制/转置到单个“日期”列中。然后我扩展元数据,以便为每个日期定义相同的数据。

我有一个单独的工作簿,它从每个工作簿中获取合并工作表,并从中构建一个数据透视表。

它确实有效,但效率相当低,因为任务/分配的总数有数千个。在我的梦想中,我希望完全消除整合步骤,但我不认为这种情况会发生。更有效的整合方法是我目前所希望的最好方法。

如果有人有一些“跳出框框”的想法,我洗耳恭听! 该解决方案需要在 Windows XP、Office 2002 和 2003 上运行。

I have a customer who is currently using Excel to do their staff planning. They have many workbooks for different projects and each project contains 1 or more sheets containing the actual staffing data:

Sample staff planning sheet

The customer wants to consolidate all of the data from all of these many sheets and workbooks into a single pivot table. A 'consolidated' pivot is not an option because they want to be able to mess with all of the (non-date) fields in the source data. They don't want to be limited to only 'Row' and 'Column'. My current solution is a macro that consolidates all of the data within a workbook through a fairly convoluted copy and rotate process. I copy a row of 'meta data' (everything that's not a date) first, then I copy / transpose the dates for the meta data row into a single 'Date' column. Then I extend the meta data so that the same data is defined for each date.

I have a separate workbook that grabs the consolidated sheet from each workbook and builds a single pivot table from them.

It works, but it's pretty inefficient, since the total number of tasks / assignments number in the many thousands. In my dreams, I would love to eliminate the consolidation step completely, but I don't see that happening. A more efficient consolidation approach is about the best I'm hoping for at this point.

If anyone has some 'outside the box' ideas, I'm all ears!
The solutions needs to work on windows XP, Office 2002 and 2003.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新一帅帅 2024-08-11 18:18:43

我终于找到了一个可接受的解决方案,如果有人感兴趣的话。它使用数据透视表和 TextToColumns< /a> 函数。一旦我掌握了这种方法,将其转化为代码就非常简单了。下面的代码确实引用了我使用的一些便利函数,例如“DeleteSheet”和“LastRowOn”,但您明白了。

Sub Foo()
    Dim ws As Worksheet
    For Each ws In Worksheets
        If IsStaffingSheet(ws) Then
            ws.Select
            DeleteSheet ws.Name & " - Exploded"
            TransposeSheet ws
        End If
    Next ws

End Sub

Sub TransposeSheet(ByVal ParentSheet As Worksheet)
    Dim ws As Worksheet
    Dim r As Range
    Dim ref As Variant
    Dim pt As PivotTable

    Set r = Range("StaffingStartCell")
    Set r = Range(r, r.SpecialCells(xlLastCell))

    ref = Array("'" & ActiveSheet.Name _
                    & "'!" & r.Address(ReferenceStyle:=xlR1C1))

    Application.CutCopyMode = False
    ActiveWorkbook.PivotCaches.Add(SourceType:=xlConsolidation, _
                                   SourceData:=ref).CreatePivotTable TableDestination:="", _
        tableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10

    Set ws = ActiveSheet
    Set pt = ws.PivotTableWizard(TableDestination:=ActiveSheet.Cells(3, 1))
    pt.DataPivotField.PivotItems("Count of Value").Position = 1
    pt.PivotFields("Row").PivotItems("").Visible = False

    ExplodePivot ParentSheet
    Application.DisplayAlerts = False
    ws.Delete
    Application.DisplayAlerts = True

    Set ws = Nothing
End Sub


Sub ExplodePivot(ByVal ParentSheet As Worksheet)
    Dim lastRow As Long
    Dim lastCol As Long

    lastRow = LastRowOn(ActiveSheet.Name)
    lastCol = LastColumnBack(ActiveSheet, lastRow)

    Cells(lastRow, lastCol).ShowDetail = True

    Columns("B:C").Select
    Selection.Cut Destination:=Columns("S:T")

    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), _
                            DataType:=xlDelimited, _
                            Semicolon:=True
    Selection.ColumnWidth = 12
    ActiveSheet.Name = ParentSheet.Name & " - Exploded"
End Sub

I finally found an acceptable solution, if anyone is interested. It's uses a combination of a Pivot Table and the TextToColumns function. Once I had the approach, turing it into code was pretty simple. The code below does refer to a few conveniance functions I use, such as 'DeleteSheet' and 'LastRowOn', but you get the idea.

Sub Foo()
    Dim ws As Worksheet
    For Each ws In Worksheets
        If IsStaffingSheet(ws) Then
            ws.Select
            DeleteSheet ws.Name & " - Exploded"
            TransposeSheet ws
        End If
    Next ws

End Sub

Sub TransposeSheet(ByVal ParentSheet As Worksheet)
    Dim ws As Worksheet
    Dim r As Range
    Dim ref As Variant
    Dim pt As PivotTable

    Set r = Range("StaffingStartCell")
    Set r = Range(r, r.SpecialCells(xlLastCell))

    ref = Array("'" & ActiveSheet.Name _
                    & "'!" & r.Address(ReferenceStyle:=xlR1C1))

    Application.CutCopyMode = False
    ActiveWorkbook.PivotCaches.Add(SourceType:=xlConsolidation, _
                                   SourceData:=ref).CreatePivotTable TableDestination:="", _
        tableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10

    Set ws = ActiveSheet
    Set pt = ws.PivotTableWizard(TableDestination:=ActiveSheet.Cells(3, 1))
    pt.DataPivotField.PivotItems("Count of Value").Position = 1
    pt.PivotFields("Row").PivotItems("").Visible = False

    ExplodePivot ParentSheet
    Application.DisplayAlerts = False
    ws.Delete
    Application.DisplayAlerts = True

    Set ws = Nothing
End Sub


Sub ExplodePivot(ByVal ParentSheet As Worksheet)
    Dim lastRow As Long
    Dim lastCol As Long

    lastRow = LastRowOn(ActiveSheet.Name)
    lastCol = LastColumnBack(ActiveSheet, lastRow)

    Cells(lastRow, lastCol).ShowDetail = True

    Columns("B:C").Select
    Selection.Cut Destination:=Columns("S:T")

    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), _
                            DataType:=xlDelimited, _
                            Semicolon:=True
    Selection.ColumnWidth = 12
    ActiveSheet.Name = ParentSheet.Name & " - Exploded"
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文