Run-time error '1004: Subtotal method of range

2019-08-02 20:57发布

I am trying to run a subtotal on a specific sheet (List) using the following code;

ActiveWorkbook.Sheets("List").Activate

Columns("A:G").Select

Selection.Subtotal GroupBy:=7, Function:=xlSum, TotalList:=Array(4, 5, 6)

However keep getting the following error (highlighting the final row of code);

Run-time error '1004:

Subtotal method of Range class failed

Any help would be much appreciated!

Thanks

2条回答
Melony?
2楼-- · 2019-08-02 21:22

I don't believe you're working on the worksheet that you think you are. To confirm this, replace

ActiveWorkbook.Activate

with

ActiveWorkbook.Select

They are not equivalent. In order to avoid this in the future, explicity work on your desired sheet like this:

Dim ws as Worksheet
Set ws = ThisWorkbook.Sheets("List")

ws.Columns("A:G").Subtotal GroupBy:=7, Function:=xlSum, TotalList:=Array(4, 5, 6)

This question has some good answers indicating what I believe to be your issue. Particularly this one.

查看更多
贪生不怕死
3楼-- · 2019-08-02 21:46

I have this error on Delphi, the decision has helped is setting of numerical values ​​for xlSum = -4157 and xlSummaryBelow = 1 (look here https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-subtotal-method-excel)

查看更多
登录 后发表回答