Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
楼主: mayldx

[已解决]宏录制透视表

[复制链接]
 楼主| 发表于 2013-6-25 14:51 | 显示全部楼层
hwc2ycy 发表于 2013-6-25 14:46
等会帮你改,现在有点事。

好的,谢谢,我不急,你先忙。
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2013-6-25 15:19 | 显示全部楼层
  1. Sub test()
  2.     With Application
  3.         .ScreenUpdating = False
  4.         .DisplayAlerts = False
  5.         .EnableEvents = False
  6.         .Calculation = xlCalculationManual
  7.     End With

  8.     Sheet1.Activate
  9.     If Range("a1").Value <> "货号" Then
  10.         Rows("1:4").Delete Shift:=xlUp
  11.         Columns("C:P").Delete Shift:=xlToLeft
  12.         Range("D:D,A:A").Delete Shift:=xlToLeft
  13.         With Columns("B:B")
  14.             .Insert Shift:=xlToRight
  15.             .Insert Shift:=xlToRight
  16.             .Insert Shift:=xlToRight
  17.         End With
  18.         Columns("B:D").NumberFormatLocal = "G/通用格式"
  19.         Range("B2").FormulaR1C1 = "=LEFT(RC[-1],1)"
  20.         Range("C2").FormulaR1C1 = "=MID(RC[-2],2,1)"
  21.         Range("D2").FormulaR1C1 = "=MID(RC[-3],3,1)"

  22.         Range("B2:D2").AutoFill Destination:=Range("B2:D737")
  23.         Range("B1").FormulaR1C1 = "品牌"
  24.         Range("C1").FormulaR1C1 = "年度"
  25.         Range("D1").FormulaR1C1 = "季度"
  26.         Columns("A:E").EntireColumn.AutoFit
  27.     End If
  28.     With ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=ActiveSheet.UsedRange.Address)
  29.         With .CreatePivotTable(TableDestination:="")
  30.             With .PivotFields("品牌")
  31.                 .Orientation = xlRowField
  32.                 .Position = 1
  33.             End With
  34.             With .PivotFields("年度")
  35.                 .Orientation = xlRowField
  36.                 .Position = 2
  37.             End With
  38.             With .PivotFields("季度")
  39.                 .Orientation = xlRowField
  40.                 .Position = 3
  41.             End With
  42.             .AddDataField .PivotFields("合计"), "求和项:合计", xlSum
  43.             .TableStyle2 = ""
  44.             .RowAxisLayout xlTabularRow
  45.             .ShowDrillIndicators = False
  46.         End With
  47.     End With
  48.     With Application
  49.         .ScreenUpdating = True
  50.         .DisplayAlerts = True
  51.         .EnableEvents = True
  52.         .Calculation = xlCalculationAutomatic
  53.     End With
  54.     MsgBox "OK"
  55. End Sub
复制代码
回复

使用道具 举报

发表于 2013-6-25 15:19 | 显示全部楼层
季度比你多出来的项目你可以筛选掉的。
回复

使用道具 举报

发表于 2013-6-25 15:20 | 显示全部楼层
本帖最后由 hwc2ycy 于 2013-6-25 15:22 编辑

筛选季度字段的代码,可根据实际需要改下。
  1.     With .PivotFields("季度")
  2.         .PivotItems("A").Visible = False
  3.         .PivotItems("B").Visible = False
  4.         .PivotItems("(blank)").Visible = False
  5.     End With
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-6-25 16:07 | 显示全部楼层
With ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=ActiveSheet.UsedRange.Address)
我把公式输入到宏里面  提示这个变黄了,里面的语句不太明白,不知道操作不对。
回复

使用道具 举报

 楼主| 发表于 2013-6-25 16:22 | 显示全部楼层
hwc2ycy 发表于 2013-6-25 15:20
筛选季度字段的代码,可根据实际需要改下。

With ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=ActiveSheet.UsedRange.Address)
我把公式输入到宏里面  这个代码提示这个变黄了,里面的语句不太明白,不知道哪里操作不对。
回复

使用道具 举报

发表于 2013-6-25 16:31 | 显示全部楼层
你用的什么版本,我传个附件给你吧。
回复

使用道具 举报

 楼主| 发表于 2013-6-25 16:42 | 显示全部楼层
hwc2ycy 发表于 2013-6-25 16:31
你用的什么版本,我传个附件给你吧。

好啊,我的是2003的
回复

使用道具 举报

发表于 2013-6-25 16:45 | 显示全部楼层
我发现个怪事情,第一次用代码生成的总是会有数据不全,再运行一次就好了。
回复

使用道具 举报

发表于 2013-6-25 16:46 | 显示全部楼层
原来刷新就好了,{:1012:}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-3 04:56 , Processed in 0.310556 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表