Sumproduct in VBA with a Space

Tinkerz

Board Regular
Joined
Feb 26, 2007
Messages
179
The Excel spreadsheet I use the formula
'=SUMPRODUCT((Accounts!S2:S65536= 1) * (Accounts!R2:R65536 = """ """))

Then I include it in VBA to run across a select number of sheets, I use the ws.new in the worksheet name to locate the file, this works as I have taken out some more simple code in the below loop

The error is a type mismatch but the formula looks ok to me, same as the one in excel, any other eyes on it would be great

Thanks


Dim wsnew As Worksheet
offsetcounter = 0
For Each wsnew In Worksheets
If InStr(1, wsnew.Name, "Accounts") > 0 Then

Application.WorksheetFunction.SumProduct((Worksheets(wsnew.Name).Range("S:S") = 1) * (Worksheets(wsnew.Name).Range("R:R") = """ """))
offsetcounter = offsetcounter + 1
End If
Next wsnew
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi Tinkerz,

If the SUMPRODUCT spreedsheet formula works correctly for you, and the VBA SumProduct fails, try to
use the same spreedsheet formula within VBA as follow example:

Code:
Range("A1") = Evaluate("=SUMPRODUCT((Accounts!S2:S65536= 1) * (Accounts!R2:R65536 = """ """))")
Hope this helps,

Regards
 
Upvote 0
I get an compile error
Expected end of list or separater

I seem to have the right amount of Brackets and speech commas


Worksheets("Calculations").Range("E1") = Evaluate("=SUMPRODUCT((Accounts!S2:S65536= 1) * (Accounts!R2:R65536 = """ """))")

I am also trying
'Worksheets("Calculations").Range("C6").Offset(0, offsetcounter) = Application.WorksheetFunction.SumProduct((Worksheets(wsnew.Name).Range("S:S") = 1) * (Worksheets(wsnew.Name).Range("R:R") = """Chr(32)"""))

Chr(32)

I am trying to lookup " ", 2 speech marks and a space so googling it looks like """ """"

I am not sure on excel VBA and space, so i would like to put in CHR(32) instead of a blank space.

But it produces type mis match in runtime

How do i solve it?
 
Upvote 0
even a simple

Worksheets("Calculations").Range("E1").Formula = "SUMPRODUCT((Accounts!S2:S65536= 1) * (Accounts!R2:R65536 = """ """))"


is producing a Compile error

Expected: End of statement
 
Upvote 0
Hi Tinkers,

Try SUMPRODUCT or SUM as examples below:

Excel Workbook
ABCDEF
11dataUsing FUNCTIONSUMPRODUCT()SUM()
22Result22
35data
41
57data
67
71data
81
98
(SUM() formula must be enter as array formula, with Ctrl+Shift +Enter)
Excel 2010
Cell Formulas
RangeFormula
E2=SUMPRODUCT(--($A$1:$A$9=1),--($B$1:$B$9=""))
#VALUE!
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}. Note: Do not try and enter these manually yourself

 
Upvote 0
Try

Worksheets("Calculations").Range("E1").Value = Evaluate("=SUMPRODUCT((Accounts!S2:S65536= 1) * (Accounts!R2:R65536 = "" ""))")
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,735
Members
452,939
Latest member
WCrawford

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