Dashes in Formulas "--"

FinancialAnalystKid

Well-known Member
Joined
Oct 14, 2004
Messages
779
I ran into this Formula on VBAExpress

=SUMPRODUCT(--($B$2:$B$8=F2),--($D$2:$D$8="X"))

And I was wondering what the dashes '--' do in a formula? I've never come across using -- in a formula before.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
It's called a double unary minus. Minus minus is the same as plus. It coerces a TRUE/FALSE boolean into 1/0. The same could be achieved by multiplying by 1 or adding zero. But -- is in vogue at the moment because apparently it is a nanosecond or two quicker than the alternatives.
 
Upvote 0
So this formula:

=SUMPRODUCT(--($B$2:$B$8=F2),--($D$2:$D$8="X"))

could be written:

=SUMPRODUCT(1*($B$2:$B$8=F2),1*($D$2:$D$8="X"))

I don't understand how this works.
 
Upvote 0
It would more likely be written:

=SUMPRODUCT(($B$2:$B$8=F2)*($D$2:$D$8="X"))

It compares each cell in B2:B8 with F2 and returns an array of TRUE/FALSE values. Then it compares each cell in D2:D8 with "X" and returns another (equally sized) array of TRUE/FALSE values. The 2 arrays are multiplied together, and in the process TRUE is coerced to 1 and FALSE to zero. That results in an array of 1/0 values (1*1=1, 1*0=0, 0*1=0, 0*0=0). This array is then summed. The effect is a count of the two conditions being TRUE.
 
Upvote 0
In the -- example there are two arrays separated by a comma, ie SUMPRODUCT has two arguments. Each array is converted to 1/0 by -- and the 2 arrays are multiplied. In the second example there is only one argument, which comes about by multiplying the arrays together. It's a subtle difference, but if the formula were:

=SUMPRODUCT(($B$2:$B$8=F2),($D$2:$D$8="X"))

the SUMPRODUCT function per se would not coerce the booleans to values when multiplying its 2 arguments. The coercion has to done for it, either by -- or by multiplication (the latter being a feature of Excel's calculation engine).
 
Upvote 0

Forum statistics

Threads
1,214,545
Messages
6,120,132
Members
448,947
Latest member
test111

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