vba 报错

ireance 2009-12-07 03:53:03
我有个名为PIVO_DETAIL的SHEET。
它有个名为PIVO_refresh的方法,如下:
Sub PIVO_refresh()

PIVO_DETAIL.Select
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh

With ActiveSheet.PivotTables("PivotTable1").PivotFields("AAAA")
.PivotItems("#N/A").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("BBB")
.PivotItems("#N/A").Visible = False
End With

Range("A5").Select
Selection.sort Order1:=xlAscending, Type:=xlSortLabels, OrderCustom:=1, _
Orientation:=xlTopToBottom, SortMethod:=xlPinYin

Range("B5").Select
Selection.sort Order1:=xlAscending, Type:=xlSortLabels, OrderCustom:=1, _
Orientation:=xlTopToBottom, SortMethod:=xlPinYin

End Sub
我在其他SHEET里调用PIVO_DETAIL。PIVO_refresh,老是报错。
经过单步调试,发现是PIVO_refresh里面的
Selection.sort Order1:=xlAscending, Type:=xlSortLabels, OrderCustom:=1, _
Orientation:=xlTopToBottom, SortMethod:=xlPinYin
这一句执行完后就报错
报的错是 1004错误
显示错误信息是: Application-defined or object-defined error.


Range("A5").Select
Selection.sort Order1:=xlAscending, Type:=xlSortLabels, OrderCustom:=1, _
Orientation:=xlTopToBottom, SortMethod:=xlPinYin

Range("B5").Select
Selection.sort Order1:=xlAscending, Type:=xlSortLabels, OrderCustom:=1, _
Orientation:=xlTopToBottom, SortMethod:=xlPinYin
这两句注释掉就不报错了。
请高手们帮忙看看到底是为什么?该怎么改???多谢了。
...全文
183 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
okexcel 2009-12-16
  • 打赏
  • 举报
回复
在程序中。99%的地方可以把.Select 去掉

Range("A5").sort Order1:=xlAscending, Type:=xlSortLabels, OrderCustom:=1, _
Orientation:=xlTopToBottom, SortMethod:=xlPinYin

有些东西不一定要选择上才可以操作!
liujiaqiid 2009-12-10
  • 打赏
  • 举报
回复
估计是selection对象 没指定好

不同的表要处理的话 最好先加上这么一句


Sheets("新表").Activate

Range("A5").Select


vba编程 要注意其中对象的指向
shan1119 2009-12-08
  • 打赏
  • 举报
回复
ActiveSheet.Range("A5").Select
ActiveSheet.Range("B5").Select
laoyebin 2009-12-07
  • 打赏
  • 举报
回复
没附件,不好猜

可能是Range("B5")没指定哪个表,或是排序后会改变透视表的格式

5,139

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 Office开发/ VBA
社区管理员
  • Office开发/ VBA社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧