Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 57
Default Count match of two criteria

I'm trying to write a VBA function to count the number of matches to two
criteria.
In a worksheet I can use SUMPRODUCT(--(range1="value1"),--(range2="value2"))
but I can't make it work in VBA.

range1 and range2 and value1 are variables, value2 is a string constant.

answer = Application.WorksheetFunction.SumProduct("--(" & range1 & "=""" &
value1 & """)", "--(" & range2 & "=""value2"")")

I am getting type mismatch errors. I suspect the problem is range1 and
range2 ; on a worksheet they are in the form A2:A300 but in VBA they aren't.

How to sort this out? Or is there a better method?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Count match of two criteria


Dim sformula As String

sformula = "SumProduct(--(" & range1.Address & "=""" & value1 & """)," &
_
"--(" & range2.Address & "=""" & value2 & """))"

answer = Evaluate(sformula)

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Adrian" wrote in message
...
I'm trying to write a VBA function to count the number of matches to two
criteria.
In a worksheet I can use

SUMPRODUCT(--(range1="value1"),--(range2="value2"))
but I can't make it work in VBA.

range1 and range2 and value1 are variables, value2 is a string constant.

answer = Application.WorksheetFunction.SumProduct("--(" & range1 & "=""" &
value1 & """)", "--(" & range2 & "=""value2"")")

I am getting type mismatch errors. I suspect the problem is range1 and
range2 ; on a worksheet they are in the form A2:A300 but in VBA they

aren't.

How to sort this out? Or is there a better method?




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Count match of two criteria

Array evaluation of this type is not supported in VBA. You have to use the
evaluate function to ask Excel to evaluate the formula for you:


answer = Evaluate("SumProduct(--(" & range1.Address & "=""" & _
value1 & """),--(" & range2.Address & "=""value2"")")

This will evaluate the activesheet using the array formula

--
Regards,
Tom Ogilvy



"Adrian" wrote in message
...
I'm trying to write a VBA function to count the number of matches to two
criteria.
In a worksheet I can use

SUMPRODUCT(--(range1="value1"),--(range2="value2"))
but I can't make it work in VBA.

range1 and range2 and value1 are variables, value2 is a string constant.

answer = Application.WorksheetFunction.SumProduct("--(" & range1 & "=""" &
value1 & """)", "--(" & range2 & "=""value2"")")

I am getting type mismatch errors. I suspect the problem is range1 and
range2 ; on a worksheet they are in the form A2:A300 but in VBA they

aren't.

How to sort this out? Or is there a better method?




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 57
Default Count match of two criteria

Tom, and Bob,
Many thanks to you both.

"Tom Ogilvy" wrote:

Array evaluation of this type is not supported in VBA. You have to use the
evaluate function to ask Excel to evaluate the formula for you:


answer = Evaluate("SumProduct(--(" & range1.Address & "=""" & _
value1 & """),--(" & range2.Address & "=""value2"")")

This will evaluate the activesheet using the array formula

--
Regards,
Tom Ogilvy



"Adrian" wrote in message
...
I'm trying to write a VBA function to count the number of matches to two
criteria.
In a worksheet I can use

SUMPRODUCT(--(range1="value1"),--(range2="value2"))
but I can't make it work in VBA.

range1 and range2 and value1 are variables, value2 is a string constant.

answer = Application.WorksheetFunction.SumProduct("--(" & range1 & "=""" &
value1 & """)", "--(" & range2 & "=""value2"")")

I am getting type mismatch errors. I suspect the problem is range1 and
range2 ; on a worksheet they are in the form A2:A300 but in VBA they

aren't.

How to sort this out? Or is there a better method?





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Count if two columns match different criteria Curt D. Excel Worksheet Functions 5 May 14th 23 07:44 PM
Count how many criteria in a column match criteria in another colu Charles Stover Excel Discussion (Misc queries) 3 March 6th 09 08:39 PM
Match Criteria & Return Sequential Count Sam via OfficeKB.com Excel Worksheet Functions 8 February 11th 08 03:39 AM
Count entries in a range that match a certain criteria within datavalidation. [email protected] Excel Discussion (Misc queries) 3 February 6th 08 04:35 AM
Count rows that match 3 sets of criteria? EricE Excel Worksheet Functions 3 December 29th 05 04:26 PM


All times are GMT +1. The time now is 03:10 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"