枢轴行为在宏观中 [英] Pivot behavoir in macro

查看:81
本文介绍了枢轴行为在宏观中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我录制了一个宏来创建数据透视表。宏记录正常,枢轴也看起来没问题。

I have recorded a macro to create a pivot table. Macro recorded ok, and the pivot also looking ok.

exec文件包含2列,我想使用字段作为数据透视表中的行标签和xlcount in值区域。

The exec file contains 2 columns and I want to use on of the fields as the the row label in the pivot and as the xlcount in the values area.

手动执行此操作即可。通过宏执行此操作时,创建了轴,行标签是创建的,但是当在值区域中添加xlcount时,行标签会移动到脚值区域,从而为我留下没有行的数据透视表。

When doing this by hand it is ok. When doing this via the macro, The pivot is creates, The row labels are create, but when adding the xlcount in the values area the row labels move toe values area leaving me with a pivot tabel with no rows.

有没有人有任何建议。

 

我使用的宏代码如下。

Sub Macro1()

'

'Macro1 Macro

'¥b
$ b'

    Cells.Select

    Sheets.Add

    ActiveWorkbook.PivotCaches.Create(SourceType中:= xlDatabase,SourceData:= _

       " SCOM_MP_May R1C1:R1048576C2" ;,版本:= xlPivotTableVersion14 )。 _

        CreatePivotTable TableDestination:=" Sheet1!R3C1",TableName:=" PivotTable2" _

        ,DefaultVersion:= xlPivotTableVersion14

   表格("Sheet1")。选择

   单元格(3,1)。选择

   使用ActiveSheet.PivotTables("PivotTable2")。PivotFields("主机")

        .Orientation = xlRowField

        .Position = 1

   结束与$
    ActiveSheet.PivotTables(QUOT; PivotTable2")AddDataField ActiveSheet.PivotTables(_

       " PivotTable2")。透视字段(QUOT;主机" ),"主持人数",xlCount

   使用ActiveSheet.PivotTables("PivotTable2")。PivotFields("MP")

        .Orientation = xlColumnField

        .Position = 1

   结束与$
结束子

$

Sub Macro1()
'
' Macro1 Macro
'

'
    Cells.Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "SCOM_MP_May!R1C1:R1048576C2", Version:=xlPivotTableVersion14). _
        CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable2" _
        , DefaultVersion:=xlPivotTableVersion14
    Sheets("Sheet1").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Host")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
        "PivotTable2").PivotFields("Host"), "Count of Host", xlCount
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("MP")
        .Orientation = xlColumnField
        .Position = 1
    End With
End Sub

推荐答案

我发现记录一个枢轴令人困惑。 只需研究代码并编写自己的代码就更容易了。 示例:

I find recording a pivot confusing.  It is easier to just study the code and write your own.  Example:

 

      '选择数据透视表的数据范围



      'wsR =包含数据的工作表

      'ptWS =数据透视表的工作表

    设置pivotRng = wsR.Range(wsR.Cells(1,1),wsR.Cells(lastRow,lastCol))

      '创建数据透视表

     设置PTCache = ActiveWorkbook.PivotCaches.Add _

                &NBSP ;   (SourceType中:= xlDatabase,_

                 &NBSP ;   SourceData:= pivotRng)

 

      'ptRow =要付PT的行。
     设置PTABLE = PTCache.CreatePivotTable(TableDestination:= ptWS.Range(ptWS.Cells(ptRow,1),ptWS.Cells(ptRow,1)),表名:=" ExamplePivot")

&NBSP ;    使用PTable

        .PivotFields(" COLUMN_NAME1")。Orientation = xlRowField

        .PivotFields(" COLUMN_NAME2")。Orientation = xlRowField

        .PivotFields(" COLUMN_NAME3"。)Orientation = xlColumnField

        .AddDataField .PivotFields(" COLUMN_NAME4")," COLUMN_NAME4的总和",xlSum

     结束

      ' Select data range for pivot table

      'wsR = worksheet with data
      'ptWS = worksheet for pivot table
     Set pivotRng = wsR.Range(wsR.Cells(1, 1), wsR.Cells(lastRow, lastCol))
      ' Create Pivot Table
      Set PTCache = ActiveWorkbook.PivotCaches.Add _
                    (SourceType:=xlDatabase, _
                     SourceData:=pivotRng)
 
      ' ptRow = row to put PT
      Set PTable = PTCache.CreatePivotTable(TableDestination:=ptWS.Range(ptWS.Cells(ptRow, 1), ptWS.Cells(ptRow, 1)), tableName:="ExamplePivot")
      With PTable
        .PivotFields("COLUMN_NAME1").Orientation = xlRowField
        .PivotFields("COLUMN_NAME2").Orientation = xlRowField
        .PivotFields("COLUMN_NAME3").Orientation = xlColumnField
        .AddDataField .PivotFields("COLUMN_NAME4"), "Sum of COLUMN_NAME4", xlSum
      End With


这篇关于枢轴行为在宏观中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆