数据透视表 - VBA宏

时间:2014-09-14 22:26:45

标签: excel vba

我正在尝试使用vba宏创建数据透视表。我可以创建初始数据透视表(图像1),但是当我尝试修改排列(图像2)时,我收到错误。

啊哈!明白了!

.Orientation = xlRowField

这是关键,并使用1-n表示事件数量。

'Setting Fields
With pt
With .PivotFields("ID")
.Orientation = xlRowField
.Position = 1
End With

'set column field
With .PivotFields("WorkTime")
.Orientation = xlRowField
.Function = xlSum
.Position = 2
End With

'set column field
With .PivotFields("OvertimePeriod")
.Orientation = xlRowField
.Function = xlSum
.Position = 3
End With

With .PivotFields("Sick leave")
.Orientation = xlRowField
.Function = xlSum
.Position = 4
End With

With .PivotFields("Vacation")
.Orientation = xlRowField
.Function = xlSum
.Position = 5
End With

如图2所示,它们并非全部排成一线!

任何建议都将不胜感激。谢谢你提前。

Ĵ

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试使用Excel中的录制宏录制它。 click the gif image for the steps