vba excel按缓存或新建创建数据透视表

时间:2013-04-04 12:33:11

标签: excel vba excel-vba excel-2010

在vba excel宏工具上,我需要从两个数据表中创建许多新的工作表和数据透视表。

我们有两种方式

  1. 使用pivot cache创建新的数据透视表。
  2. 一次又一次地创建具有相同数据表的新数据透视表。
  3. 如果我选择第二种方法会有什么影响,

    这会使我们的excel文件更重吗?

    想跳过第一种方法,因为如果需要依赖第一个数据透视表应该存在于每个excel工作簿中?

    使用excel 2010。

1 个答案:

答案 0 :(得分:0)

我做了一个项目,它将在现有工作表中创建数据透视表,并将数据复制到新的电子表格中。如果这是你的问题..请参考下面的代码

Dim objtable as pivottable, objfield as pivotfield
set objtable = sheet1.pivotwizard
   ptname = activesheet.pivottables(1).name 'this code is to give pivot table name which will help in creating new pivot tables
   set objfield.objtable.pivotfields("Month") 'this code will select month in page field
 objfield.orientation = xlpagefield
set objfield = objtable.pivotfields ("Dept & Mgr") 'this code will select dept & mgr in page field
objfield.orientation = xlpagefield
set objfield = objtable.pivotfields("Legal Entity") 'this code will select legal entity in row field
objfield.orientation = xlrowfield
set objfield = objtable.pivotfields("Cost Code") 'this code will select cost code in row field
objfield.orientation = xlrowfield
set objfield = objtable.pivotfields("CC Description") 'this code will select CC Description in row field
objfield.orientation = xlrowfield
set objfield = objtable.pivotfields("User") 'this code will select Usr in row field
objfield.orientation = xlrowfield
set objfield = objtable.pivotfields("Carrier") 'this code will select carrier in row field
objfield.orientation = xlrowfield
set objfield = objtable.pivotfields("Description") 'this code will select Description in row field
objfield.orientation = xlrowfield
set objfield = objtable.pivotfields("Amount Incl Tax") 'this code will amount incl tax in Datafield
objfield.orientation = xlDatafield
activesheet.pivottables(ptname).format xlreport6 '