Excel VBA从表创建数据透视表

时间:2017-10-20 14:44:28

标签: excel vba excel-vba

因此,我是VBA的新手,并试图在工作项目中出现一些事情,这让我疯狂。

我有一个宏来清理我的数据并将其放入表中然后我正在处理一个宏来获取该表并将其转换为数据透视表/图表。第一个宏在电子表格" DailyData"上创建Table1。作为参考,这里是:

Sub Format_Table()
Dim target As Worksheet
Set target = ThisWorkbook.Worksheets(1)
target.Name = "DailyData"
Worksheets("DailyData").Range("AB:AB,O:P,C:K,A:A").EntireColumn.Delete
Worksheets("DailyData").UsedRange
Worksheets("DailyData").UsedRange.Select
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes)
tbl.TableStyle = "TableStyleMedium15"
tbl.HeaderRowRange.Cells(1, 1).Value = "Date"
tbl.HeaderRowRange.Cells(1, 3).Value = "Machine"
Worksheets("DailyData").Cells.EntireColumn.AutoFit
End Sub

我的Pivot理想情况下会有" Part Number"作为一个行字段,然后"设置时间" "间接劳动" "劳动时间" "标准劳动时间" "劳动效率"作为价值领域。

到目前为止,我还没有能够获得其中一个Value字段,而不是单独显示所有5.所以我当前尝试让第一个值字段显示正确如下:

Sub sbCreatePivot()
Dim ws As Worksheet
Dim pc As PivotCache
Dim pt As PivotTable
Set ws = Worksheets.Add
Set pc = ActiveWorkbook.PivotCaches.Create(xlDatabase, "DailyData!Table1")
Set pt = pc.CreatePivotTable(ws.Range("B3"))
With pt
    With .PivotFields("Part Number")
       .Orientation = xlRowField
       .Position = 1
    End With
    With .PivotFields("Labour Hours")
        .Orientation = xlColumnField
        .Position = 1
    End With
    .AddDataField .PivotFields("Labour Hours"), "Labour Hours", xlSum
End With

End Sub

我真的很感谢帮助,因为我对这一切都很陌生,并且在这里尽力而为,但我只是在出错后继续遇到错误,并且没有办法谷歌了。

谢谢, 泰勒

2 个答案:

答案 0 :(得分:0)

试试这个替换

.AddDataField .PivotFields("Labour Hours"), "Labour Hours", xlSum

用这个

With .PivotFields("Labour Hours")
    .Orientation = xlDataField
    .Function = xlSum
    .NumberFormat = "0"
End With

答案 1 :(得分:0)

尝试下面的代码,我对两个函数进行了修改,以更好地利用Excel的VBA功能:

Sub Format_Table()

Dim targetSht As Worksheet
Dim tbl As ListObject

Set targetSht = ThisWorkbook.Worksheets(1)
targetSht.Name = "DailyData"

With targetSht
    .Range("AB:AB,O:P,C:K,A:A").EntireColumn.Delete

    Set tbl = .ListObjects.Add(SourceType:=xlSrcRange, Source:=.UsedRange, XlListObjectHasHEaders:=xlYes)
End With

With tbl
    .TableStyle = "TableStyleMedium15"
    .HeaderRowRange.Cells(1, 1).Value = "Date"
    .HeaderRowRange.Cells(1, 3).Value = "Machine"
End With

targetSht.Cells.EntireColumn.AutoFit

End Sub
Sub sbCreatePivot()

Dim ws As Worksheet
Dim pc As PivotCache
Dim pt As PivotTable
Dim targetSht As Worksheet
Dim tbl As ListObject

' set the target sheet
Set targetSht = ThisWorkbook.Worksheets("DailyData")
' set the Table (listObject)
Set tbl = targetSht.ListObjects("Table1")

Set ws = Worksheets.Add
' Modified line to set the Pivot Cache
Set pc = ActiveWorkbook.PivotCaches.Create(xlDatabase, tbl.Range.Address(False, False, xlA1, xlExternal))
Set pt = pc.CreatePivotTable(ws.Range("B3"))

With pt
    With .PivotFields("Part Number")
       .Orientation = xlRowField
       .Position = 1
    End With
    With .PivotFields("Labour Hours")
        .Orientation = xlColumnField
        .Position = 1
    End With

    ' Modified line
    .AddDataField .PivotFields("Labour Hours"), "Sum of Labour Hours", xlSum
End With

End Sub