Excel does not recognize worksheetfunction.subtotal

BrianPagan

New Member
Joined
Sep 26, 2011
Messages
11
Hello!

I have many subtotals in one column and one of them in the middle is a vba formula worksheetfunction.subtotal, but excel does not know that and behaves as if it were just a number there and sums up everything. How could I make excel recognize vba subtotal?

If that is not possible, then I would like to give excel subtotal formula a range that I have defined in vba. How could I do that?

thanks!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Could you clarify exactly what you have in the cell - i.e. how you are calling the worksheetfunction.subtotal?
 
Upvote 0
I copied my function code here and it works. Just that I would like excel to know that it is subtotal.

In the cell I enter =summa_tegevus()

Function summa_tegevus()

Dim m As Integer
Dim allcells As Range

Application.Volatile
If TypeName(Application.Caller) = "Range" Then
If Application.Caller.Row <> 65536 And Application.Caller.Column <> 1 Then

m = 1

Do Until Range(Application.Caller.Offset(m, -2).Address) = ""
m = m + 1
Loop

Set allcells = Range(Application.Caller.Offset(1, 0).Address, Application.Caller.Offset(m, 0).Address)
summa_tegevus = Application.WorksheetFunction.Subtotal(9, allcells)

End If
End If

End Function
 
Upvote 0
Oh, I see what you mean - you want the SUBTOTAL function to ignore your custom function? No way to do that that I know of - it will only ignore actual SUBTOTAL formulas.
 
Upvote 0
But is there a way to giva excel formula a vba defined range?
for example: SUBTOTAL(9;allcells) where allcells is defined in vba function
 
Upvote 0
You can create a function that returns a range and then use that with this sort of syntax:
=SUBTOTAL(9;GetCells())
for example.
 
Upvote 0
=SUBTOTAL(9;GetCells(A1))

A1 =function_name that returns a range addresses. Is that what you mean?

I tried that but didnt work. I tried to find information on GetCells() in the internet, couldnt really find anything useful. Couldnt find it in excel's list of formulas either.
Could you tell me a bit more about GetCells()?

Thank You
 
Upvote 0
GetCells would be your new function that returns a range. For example:
Code:
Function GetCells() as range

Dim m As Integer
Dim allcells As Range

Application.Volatile
If TypeName(Application.Caller) = "Range" Then
If Application.Caller.Row <> 65536 And Application.Caller.Column <> 1 Then

m = 1

Do Until Range(Application.Caller.Offset(m, -2).Address) = ""
m = m + 1
Loop

Set GetCells = Application.caller.worksheet.Range(Application.Caller.Offset(1, 0).Address, Application.Caller.Offset(m, 0).Address)

End If
End If

End Function

and you then use the formula syntax I posted.
 
Upvote 0
FYI, I think you could do this without any code.
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,451
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top