Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 凌乱的透视表_Vba_Excel - Fatal编程技术网

Vba 凌乱的透视表

Vba 凌乱的透视表,vba,excel,Vba,Excel,我通过vba记录了这个数据透视表并做了一些修改。虽然我在将count id添加为值时遇到了一些问题,但效果很好。 不管怎么说,它很好用,但令我恼火的是它的外观。它既笨拙又凌乱。 你介意帮我把它弄对一点吗 代码片段: ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "id!R1C2:R65536C2", Version:=xlPivotTableVersion10). _ Create

我通过vba记录了这个数据透视表并做了一些修改。虽然我在将count id添加为值时遇到了一些问题,但效果很好。 不管怎么说,它很好用,但令我恼火的是它的外观。它既笨拙又凌乱。 你介意帮我把它弄对一点吗

代码片段:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "id!R1C2:R65536C2", Version:=xlPivotTableVersion10). _
    CreatePivotTable TableDestination:="temp!R1C1", TableName:= _
    "Tabela przestawna5", DefaultVersion:=xlPivotTableVersion10
With ws2
        .PivotTables("Tabela przestawna5").AddDataField ws2. _
            PivotTables("Tabela przestawna5").PivotFields("id"), _
            "Licznik z id", xlCount
    With ws2.PivotTables("Tabela przestawna5").PivotFields("id" _
            )
            .Orientation = xlRowField
            .Position = 1
    End With
        .Columns("A:B").Copy
        .Columns("A:B").PasteSpecial Paste:=xlPasteValues
End With
其目的是: 从id表ws1中提取一列数据,制作一个数据透视表,将其放置在临时表ws2中,并将其复制/粘贴为值,将其过滤并放入一些公式

一,。如果不选择透视表的工作表,我似乎无法对其进行任何更改-这是真的吗? 2.提到了添加计数的问题-这就是我最终单独完成的原因。 它对我来说就像一张纸:/

我是VBA新手,所以我非常感谢您的帮助


在excel 2007上工作,如果这很重要的话

以下是您的代码重新格式化:

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:="id!R1C2:R65536C2", _
        Version:=xlPivotTableVersion10).CreatePivotTable _
        TableDestination:="temp!R1C1", _
        TableName:="Tabela przestawna5", _
        DefaultVersion:=xlPivotTableVersion10


    ws2.PivotTables("Tabela przestawna5").AddDataField _
    ws2.PivotTables("Tabela przestawna5").PivotFields("id"), "Licznik z id", xlCount

    With ws2.PivotTables("Tabela przestawna5").PivotFields("id")
            .Orientation = xlRowField
            .Position = 1
    End With

    ws2.Columns("A:B").Copy
    ws2.Columns("A:B").PasteSpecial Paste:=xlPasteValues

但我真的不明白为什么AddDataField计数有问题。。。你能告诉我们更多的plz吗?

你不应该使用ActiveSheet。如果您不想只在活动工作表上操作,请在代码中输入。这就引出了您的第一个问题的答案:这是错误的,您可以对工作表上的任何对象进行操作,只要您不明确要求只对ActiveSheet进行操作;使用对工作表的引用替换ActiveSheet如果它是ws2,那么您可以忽略ActiveSheet,因为其作用域为with