Sumif with autofilter vba

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
VBA Code:
Dim My Range as Range

Set MyRange = Range("Q7", Range("Q7").End(xlDown)).SpecialCells(xlCellTypeVisible)

WorksheetFunction.SumIf(MyRange, ">0")

I have this code sums value greater than zero. I'm using an autofilter and I would like to know how I can get the results if the rows are filtered?

Thanks
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try this:

VBA Code:
Dim Answer As Double
Answer = Evaluate("SUMPRODUCT(SUBTOTAL(109,OFFSET(Q$7,ROW(MyRange)-ROW(Q$7),0))*(MyRange>0))")
 
Upvote 0
Thank you for the reply, however, I'm getting a type mismatch error.
 
Upvote 0
How about
VBA Code:
Dim MyRange As Range
Dim Ans As Double
Set MyRange = Range("Q7", Range("Q" & Rows.Count).End(xlUp))
Ans = Evaluate("SUMPRODUCT(SUBTOTAL(109,OFFSET(Q$7,ROW(" & MyRange.Address & ")-ROW(Q$7),0))*(" & MyRange.Address & ">0))")
 
Upvote 0
Solution
Works great Fluff...Thank you :)

Question...why use Evaluate and not Worksheet function?
 
Upvote 0
Question...why use Evaluate and not Worksheet function?
I actually tried the Worksheet function initially but while the Evaluate function lets you basically use the same Excel function you use for your original test build in Excel, the Worksheet function requires a lot of moving parts. ie
  • You need to know which functions you are using in the formula that require the Application.WorksheetFunction in front of it.
    As near as I can figure out in these formulas its the SumProduct and the SubTotal functions
  • Functions that have VBA equivalents don't seem to be available and you need to re-engineer them to use the VBA function.
    This seems to be the case with the Offset & Row functions
    [Excel allows Row(Range), VBA needs Range().Row]
Needless to say I could not get it working and the Evaluate function was sooo much simpler.
 
Upvote 0
I actually tried the Worksheet function initially but while the Evaluate function lets you basically use the same Excel function you use for your original test build in Excel, the Worksheet function requires a lot of moving parts. ie
  • You need to know which functions you are using in the formula that require the Application.WorksheetFunction in front of it.
    As near as I can figure out in these formulas its the SumProduct and the SubTotal functions
  • Functions that have VBA equivalents don't seem to be available and you need to re-engineer them to use the VBA function.
    This seems to be the case with the Offset & Row functions
    [Excel allows Row(Range), VBA needs Range().Row]
Needless to say I could not get it working and the Evaluate function was sooo much simpler.

Alex...I'm am greatly appreciative of your detail explanation. I actually tried the worksheet function too, but I got stuck when I kept getting an error on ROW. Needless say I came here for help. I never used evaluate before, but now I have a new tool in my toolbox :)
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

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