我对excel中的VBA编码还很陌生,我正在尝试使用VBA创建数据透视表,其中我的人员编号(人名)和员工姓名(姓氏)必须在两个以下不同的列,因此它们显示在同一行上,即每人我需要1行数据。
我在互联网上搜索了一个修复程序,并尝试了几种不同的方法来解决此问题,但是无论我做什么,它始终在同一列中输出姓名和人员编号
Sub Pivot2Create()
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
Dim wb1 As Workbook
Set wb1 = ThisWorkbook
Application.ScreenUpdating = False
'Insert a New Blank Worksheet
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Second Pivot").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "Second Pivot"
Application.DisplayAlerts = True
Set PSheet = Worksheets("Second Pivot")
Set DSheet = Worksheets("Current Report")
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="Uniper3rdParty")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="Uniper3rdParty")
'Insert Row Fields
With ActiveSheet.PivotTables("Uniper3rdParty").PivotFields("Pers.No.")
.Orientation = xlRowField
.RowAxisLayout (xlTabularRow)
.Position = 1
End With
With ActiveSheet.PivotTables("Uniper3rdParty").PivotFields("Last name First name")
.Orientation = xlRowField
.RowAxisLayout (xlTabularRow)
.Position = 2
End With
'Insert Column Fields
With ActiveSheet.PivotTables("Uniper3rdParty").PivotFields("Wage Type Long Text")
.Orientation = xlColumnField
.Position = 1
End With
'Insert Data Field
With ActiveSheet.PivotTables("Uniper3rdParty").PivotFields("Amount")
.Orientation = xlDataField
.NumberFormat = "#,##0.00"
End With
我不知道我是否已经完全搞砸了,或者我是否对代码视而不见,而只是想念一些非常简单的东西。任何人都可以提供的任何帮助将不胜感激
答案 0 :(得分:1)
尝试以下
With Psheet.PivotTables("Uniper3rdParty")
'What you asked
.RowAxisLayout xlTabularRow
'Personally, I would add the below rows but feel free to remove them
dim pf as PivotField
For Each pf In .PivotFields
pf.Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
Next pf
.ShowDrillIndicators = False
End With
答案 1 :(得分:0)
使用“经典数据透视”(并可能删除小计)
Sub FormatPivot()
With ActiveSheet.PivotTables("Uniper3rdParty")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
ActiveSheet.PivotTables("Uniper3rdParty").PivotFields("Pers.No.").Subtotals = _
Array(False, False, False, False, False, False, False, False, False, False, False, False)
End Sub