Count Diagonals

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,415
Office Version
  1. 2010
Hi everyone,</SPAN></SPAN>

The below example I have all 1 I want to count 2 Diagonal and 3 Diagonal as shown</SPAN></SPAN>

Where I need to count diagonal right side of the upper rows and left side of the lower rows</SPAN></SPAN>

For example1: if we check in row 5 we have 3 diagonal with 2 match</SPAN> (first in cell E5=1 and cell F4=1) second in cell J5=1 and cell K4=1, and last (third in cell L5=1 and M4=1) </SPAN></SPAN>

For example1: if we check in row 6 we have </SPAN>3 diagonal with 2 match and 1 with 3 match</SPAN> (first in cell E7=1 and cell E6=1) second in cell F7=1 And cell G6=1, and 3 diagonal match are In cell F7=1, cell G6=1 and Cell E8=1</SPAN></SPAN>

ABCDEFGHIJKLMNOPQ
1Game1234567891011121314Total match 2 diagonalTotal match 3 diagonal
211
32111
431111
541111113
65111
7611121
871111
981111111
109112
1110
12111111111
131211
141311112
15141

<COLGROUP><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL></COLGROUP><THEAD>
</THEAD><TBODY>
</TBODY>
Mr.Excel



Results are shown in columns P And Q</SPAN></SPAN>

Please help</SPAN></SPAN>

Thanks And Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Kind of clunky but this should work, I believe.

in R4 put =B3+C2 and fill across to AD4 and down for however long of a range you need
in AE5 put =B4+C3+D2 and fill across to AP5 and down for your whole range.

Then in P2 put =COUNTIF(R3:AD3,2)
and in Q2 put =COUNTIF(AE3:AP3,3)
and fill down for your whole range.

Cheers, :)
 
Upvote 0
Press Alt+F11 to open the VBA window then from the Insert menu choose Module, Copy the following code and paste it in the white space in front of you.
In P2 type:
=CountDiagonals2($B2:$O2)
and copy down and in Q2 type:
=CountDiagonals3($B2:$O2)
and copy down.
Code:
Function CountDiagonals2(Range As Range)
If Range Is Nothing Or Range.Rows.Count > 1 Then Exit Function
For Each Cell In Range
    If Cell.Value = 1 And Cell.Offset(-1, 1).Value = 1 And Cell.Interior.Color = Cell.Offset(-1, 1).Interior.Color Then
       i = i + 1
    End If
Next
CountDiagonals2 = i
End Function

Function CountDiagonals3(Range As Range)
If Range Is Nothing Or Range.Rows.Count > 1 Then Exit Function
For Each Cell In Range
    If Cell.Value = 1 And Cell.Offset(-1, 1).Value = 1 And Cell.Offset(1, -1).Value = 1 And Cell.Interior.Color = Cell.Offset(-1, 1).Interior.Color And Cell.Interior.Color = Cell.Offset(1, -1).Interior.Color Then
       i = i + 1
    ElseIf Cell.Value = 1 And Cell.Offset(-1, 1).Value = 1 And Cell.Offset(-2, 2).Value = 1 And Cell.Interior.Color = Cell.Offset(-1, 1).Interior.Color And Cell.Interior.Color = Cell.Offset(-2, 2).Interior.Color Then
       i = i + 1
    End If
Next
CountDiagonals3 = i
End Function
Hope you understand and I'm sure this will help you!
ZAX
 
Last edited:
Upvote 0
Another approach using Sumproduct....

In P3, and copy down
=IF(ROWS(P$2:P3)<2,0,SUMPRODUCT(--(B3:M3=1),--(C2:N2=1))-Q3)

In Q3, and copy down
=IF(ROWS(Q$2:Q3)<3,0,SUMPRODUCT(--(B3:M3=1),--(C2:N2=1),--(D1:O1=1)))
 
Upvote 0
Noticed that you want to count it in the 2 diagonal too!

DISREGARD...
 
Last edited:
Upvote 0
Another approach using Sumproduct....

In P3, and copy down
=IF(ROWS(P$2:P3)<2,0,SUMPRODUCT(--(B3:M3=1),--(C2:N2=1))-Q3)

In Q3, and copy down
=IF(ROWS(Q$2:Q3)<3,0,SUMPRODUCT(--(B3:M3=1),--(C2:N2=1),--(D1:O1=1)))

I could do it like this but wrote a macro for to include the color condition....
 
Upvote 0
Shawnhet, Thank you for your efforts it works Ok but......as you say….it is kind of clunky.</SPAN></SPAN>

Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 
Upvote 0
JS411, thank you for giving formula solution it is working as required 100% OK</SPAN></SPAN>

Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 
Upvote 0
ZAX, thank you for the function it giving result perfect. but in column Total match 3 diagonal is giving error #¡VALOR! as shown, what I am doing wrong so it is showing in some cells #¡VALOR!</SPAN></SPAN>

Please correct me.</SPAN></SPAN>
PQ
1Total match 2 diagonalTotal match 3 diagonal
20#¡VALOR!
300
40#¡VALOR!
530
60#¡VALOR!
721
81#¡VALOR!
910
102#¡VALOR!
1100
120#¡VALOR!
1300
142#¡VALOR!
1500

<COLGROUP><COL><COL><COL></COLGROUP><THEAD>
</THEAD><TBODY>
</TBODY>
Diagonal Function


Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 
Upvote 0
And one question more will it be possible to count diagonal if it is to Left or right side separately.</SPAN></SPAN>

For Example:</SPAN></SPAN>
Right side:</SPAN></SPAN>
ABCDEFGHIJ
1Game1234567Total match 2 diagonalTotal match 3 diagonal
2111
321111
4311
54111
65

<COLGROUP><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL></COLGROUP><THEAD>
</THEAD><TBODY>
</TBODY>
Right-Diagonal



Left side:</SPAN></SPAN>
ABCDEFGHIJ
1Game1234567Total match 2 diagonalTotal match 3 diagonal
2111
32111
43111
54
65

<COLGROUP><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL></COLGROUP><THEAD>
</THEAD><TBODY>
</TBODY>
Left-Diagonal



Thanks And Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 
Upvote 0

Forum statistics

Threads
1,221,550
Messages
6,160,459
Members
451,648
Latest member
SuziMacca

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