简体   繁体   English

如何将我的VBA数据透视表行数据分成两列而不是同一列?

[英]How do I get my vba pivot table row data into two columns instead of the same column?

I am fairly new to VBA coding in excel and I'm trying to create a pivot table using VBA where I have personnel number (Pers.No.) and employee name (Last name First name) which need to be in two different columns so they show on the same row ie I need 1 row of data per person. 我对excel中的VBA编码相当陌生,我正在尝试使用VBA创建数据透视表,其中我的人员编号(Pers.No.)和员工姓名(Last name First name)需要在两个不同的列中,因此它们显示在同一行上,即每人我需要1行数据。

I have searched the internet for a fix and tried several different things to fix this but no matter what I do it keeps outputting the name and personnel number in the same column 我在互联网上搜索了一个修复程序,并尝试了几种不同的方法来解决此问题,但是无论我做什么,它始终在同一列中输出姓名和人员编号

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

I do not know if I have totally screwed up or if I have gone code blind and I'm just missing something really simple. 我不知道我是否已经完全搞砸了,或者我是否对代码视而不见,而只是想念一些非常简单的东西。 Any help that anyone can provide would be greatly appreciated 任何人都可以提供的任何帮助将不胜感激

Try the following 尝试以下

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

Use a "Classic Pivot" (and possible remove subtotals) 使用“经典数据透视”(并可能删除小计)

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA excel:如何在不选择的情况下将单元格中的数据作为数组获取到同一列中的一行? - VBA excel: How do I get data in cells as an array up one row in the same column without selecting? VBA数据透视表,行数据字段未显示在同一列中 - VBA Pivot Table, Row data fields aren't showing in the same column 如何在数据透视表中隐藏总行? - How do I hide the total row in my pivot table? Excel VBA - 使用 3 个行元素和 1 个列元素从 Pivot 表中获取数据 - Excel VBA - Get data from Pivot Table using 3 row elements and 1 column element 在micosost Excel中,我如何将数据从一行中的多列复制到同一行中的一列 - In micosost excel how do i copy data from multiple columns in a row to one column in same row 获取数据 pivot 表 VBA - Get data pivot table VBA 如何使用VBA按证书总数总计列对数据透视表进行排序? - How do I sort a pivot table by certan Grand Total column using VBA? 我无法将数据字段添加到数据透视表VBA - I am unable to add Data Field to my Pivot Table VBA VBA将列数据复制到不同列的最后一行,对多列执行,遍历所有工作表,执行相同操作 - VBA Copy column data to last row of different column, do for multiple columns, loop through all worksheets do same 如何获取数据透视表中“总和”列的范围? Excel VBA - How to get the range of "Total Sum" column in a pivot table? Excel VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM