VBA- Excel formula not working in vba

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
983
Office Version
  1. 2010
Platform
  1. Windows
Hi Team


Need two help here,
First Help,


I am pasting Below excel formula into vba, its not working. it works in excel.

'=SUMPRODUCT(ISNUMBER(MATCH($A$2:$A$18,{"A","B"},0))*ISNUMBER(MATCH($B$2:$B$18,{"X","Y","Z"},0)))
'=SUMPRODUCT(ISNUMBER(MATCH($A$2:$A$18,$D$4:$D$7,0))*ISNUMBER(MATCH($B$2:$B$18,$E$4:$E$7,0)))


Sub TEST()
ActiveSheet.Range("h1").Formula = "=SUMPRODUCT(ISNUMBER(MATCH(range($A$2:$A$18),{""A"",""B""},0))*ISNUMBER(MATCH(range($B$2:$B$18),{""X"",""Y"",""Z""},0)))"
End Sub


Output formula in H1
=SUMPRODUCT(ISNUMBER(MATCH(range($A$2:$A$18),{"A","B"},0))*ISNUMBER(MATCH(range($B$2:$B$18),{"X","Y","Z"},0)))




Second Help,
If I want the result which are not available in the Criteria Array. how to use <> here.





Thanks for your help in Advance!



Regards,
mg
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hello,

You should test the Evaluate instruction ...

MsgBox Evaluate("your_formula")

Hope this will help
 
Upvote 0
Sub TEST()
ActiveSheet.Range("h1").Formula = "=SUMPRODUCT(ISNUMBER(MATCH(range($A$2:$A$18),{""A"",""B""},0))*ISNUMBER(MATCH(range($B$2:$B$18),{""X"",""Y"",""Z""},0)))"
End Sub
Your mistake is using Range(...) within the formula, as you're only using vba to enter the formula into the worksheet you need to write it as a formula, not as vba. Apart from that, the rest looks fine.

Changing ISNUMBER to ISERROR should count the <> results.
 
Upvote 0
Hi James and Jason,
Thanks for your help, Jason I used your suggested approach its working,

James - Can you please where to add Evaluate in below formula.
ActiveSheet.Range("h1").Formula = "=SUMPRODUCT(ISNUMBER(MATCH($A$2:$A$18,{""A"",""B""},0))*ISNUMBER(MATCH($B$2:$B$18,{""X"",""Y"",""Z""},0)))"


Regards,
mg
 
Upvote 0
Hi again,

Code:
Range("H1") =  Evaluate("=SUMPRODUCT(ISNUMBER(MATCH($A$2:$A$18,{""A"",""B""},0))*ISNUMBER(MATCH($B$2:$B$18,{""X"",""Y"",""Z""},0)))")

Hope this will help
 
Last edited:
Upvote 0
Hi, James and Jason


Thank you both for your help , One more help if you can,
Below formula its working Perfectely, I want to pass Criteria Dynamically.


Below formula works.
Range("H1") = Evaluate("=SUMPRODUCT(ISNUMBER(MATCH($A$2:$A$18,{""A"",""B""},0))*ISNUMBER(MATCH($B$2:$B$18,{""X"",""Y"",""Z""},0)))")


Sub Test
Dim ar1 as variant
Dim ar2 as variant


ar1 = Application.WorksheetFunction.Transpose(Range("c2:c3"))
ar1 = Application.WorksheetFunction.Transpose(Range("d2:d4"))


Getting Type mismatch Error.
Range("H1").Formula = "=SUMPRODUCT(ISNUMBER(MATCH($A$2:$A$48," & ar1 & ",0))*ISNUMBER(MATCH($B$2:$B$48," & ar2 & ",0)))"


End sub


Regards,
mg
 
Upvote 0
Hi James,

I tried both the formula, But for below formula Can you suggest where it is going wrong. Thanks.

Sub Test

Dim ar1 as variant
Dim ar2 as variant

ar1 = Application.WorksheetFunction.Transpose(Range("c2:c3"))
ar1 = Application.WorksheetFunction.Transpose(Range("d2:d4"))

Getting Type mismatch Error.
Range("H1").Formula = "=SUMPRODUCT(ISNUMBER(MATCH($A$2:$A$48," & ar1 & ",0))*ISNUMBER(MATCH($B$2:$B$48," & ar2 & ",0)))"

End sub

Thanks
mg
 
Upvote 0
I don't think that a vba array is valid as an array constant, try dropping the range address into the formula instead.

Code:
Dim rng1 As Range, rng2 As Range
Set rng1 = Range("C2:D3")
Set rng2 = Range("D2:D4")
Range("H1").Formula = "=SUMPRODUCT(ISNUMBER(MATCH($A$2:$A$48," & rng1.Address & ",0))*ISNUMBER(MATCH($B$2:$B$48," & rng2.Address & ",0)))"
 
Upvote 0
Hi Jason,


Thanks Once again for your help, learned lot of new things.
One more last and final Question , Adding one more Criteria to previous formula.


Column C Contains Numbers starting 0 to 100. I want Count Greater than 35.


What I needs add here to Make dynamic and adjust with previous two criteria.


*ISNUMBER(MATCH($C$1:$C$36,$G$2,0))))"


Range("o2").Formula = "=SUMPRODUCT((ISNUMBER(MATCH($A$1:$A$36,$E$2:$E$4,0)) _
*ISNUMBER(MATCH($B$1:$B$36,$F$2:$F$4,0)) _
*ISNUMBER(MATCH($C$1:$C$36,$G$2,0))))"

Thanks for your help in advance !!


Regards,
mg
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,189
Members
452,616
Latest member
intern444

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