Highlight 3 diagonals

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000

Hi,

I need to highlights 3 diagonals weather are they numbers or alphabets

In the example below check first column C to find diagonal in the column D & E

For example C6 is empty go for C7 if C7 is empty go for C8,
Yes find diagonal in C8, D9 & in E10 (in this case as in the row 8 find Diagonal check the complete row C8:P8 and highlight all diagonal) as found one more in the row 8 in cells M8, N10 & in O10 and continue same method to find all possible diagonal

Example data with numbers...


Book1
ABCDEFGHIJKLMNOP
1
2
3
4
5ROW NC1C2C3C4C5C6C7C8C9C10C11C12C13C14
66555
775555
8855555555
9955555555
10105555555
11115555
121255555
131355555555
14145555
151555555555
161655555555
17175555
181855555
1919555555555
2020555555
2121555555555
22225555555
23235555555
242455555555555
252555555
2626555555
272755555
2828555555555
292955555555
303055555
31315555555
32325555
333355555
34345555555
353555555555
363655555555
37375555555555
383855555555
3939555555
404055555
414155555555
4242555555
434355555555
44445555555555
45455555
46465555555
47475555555555
4848555555
494955555
505055555
51515555555555
52525555
5353555555555
54545555555
555555555
565655555555
57575555555
5858555555
59595555
60605555555
61615555
626255555555555
6363555555
6464555555
656555555555
66665555555
67675555555
6868555
696955555
7070555555
717155555
7272555555
7373555555
7474555555
757555555555
7676555555
777755
7878555
7979
8080
Sheet1


Example data with alphabets...


Book1
ABCDEFGHIJKLMNOP
1
2
3
4
5ROW NC1C2C3C4C5C6C7C8C9C10C11C12C13C14
66AAA
77AAAA
88AAAAAAAA
99AAAAAAAA
1010AAAAAAA
1111AAAA
1212AAAAA
1313AAAAAAAA
1414AAAA
1515AAAAAAAA
1616AAAAAAAA
1717AAAA
1818AAAAA
1919AAAAAAAAA
2020AAAAAA
2121AAAAAAAAA
2222AAAAAAA
2323AAAAAAA
2424AAAAAAAAAAA
2525AAAAA
2626AAAAAA
2727AAAAA
2828AAAAAAAAA
2929AAAAAAAA
3030AAAAA
3131AAAAAAA
3232AAAA
3333AAAAA
3434AAAAAAA
3535AAAAAAAA
3636AAAAAAAA
3737AAAAAAAAAA
3838AAAAAAAA
3939AAAAAA
4040AAAAA
4141AAAAAAAA
4242AAAAAA
4343AAAAAAAA
4444AAAAAAAAAA
4545AAAA
4646AAAAAAA
4747AAAAAAAAAA
4848AAAAAA
4949AAAAA
5050AAAAA
5151AAAAAAAAAA
5252AAAA
5353AAAAAAAAA
5454AAAAAAA
5555AAAAA
5656AAAAAAAA
5757AAAAAAA
5858AAAAAA
5959AAAA
6060AAAAAAA
6161AAAA
6262AAAAAAAAAAA
6363AAAAAA
6464AAAAAA
6565AAAAAAAA
6666AAAAAAA
6767AAAAAAA
6868AAA
6969AAAAA
7070AAAAAA
7171AAAAA
7272AAAAAA
7373AAAAAA
7474AAAAAA
7575AAAAAAAA
7676AAAAAA
7777AA
7878AAA
7979
8080
Sheet2


Thank you in advance

Kishan
 
Is the coloring you show in the number table example correct for the cells range H21:L24 (do you really want that lone yellow cell in cell I21)?
Hi Rick Rothstein, Thank you for asking a question here is my reply what happens and how I filled the colours when I start finding 3 diagonal manually to place my query example sheet on the "MrExcel Message Board"

How I filled the colours to highlights the diagonals I try to explain continuation...

1-I find the first set of diagonal in cells C8, D9, and E10 filled these cells with yellow colour as in row8 I find another diagonal in cells M8, N9, and O10 also filled these cells with yellow colour.

2-In the row 10, 11 and 12 find 2 diagonals filled them with another colour Cyan just to show the difference from one to another.

3-In the row 16, 17 and 18 find 2 diagonals filled them with first colour yellow.

4-here is the answer of your question finds 3 diagonal in the row 21....
1st-C21, D22, & E23, 2nd-I21, J22, & K23, 3rd-M21, N22, & O23 I did fill these cells with colour yellow.
After that I went to row 22 find 1st-C22, D23 & E24 filled with colour Cyan, find 2nd- H22, I23 & J24 filled with colour Cyan, 3rd- J22, K23 & L24 also fill with colour Cyan overwriting colour cyan yellow despaired. Now I see there is 4th-diagonal which I missed D22, E23 & F24 I would have fill Cyan too in it.

Please see the below if I would have notice of 4th diagonal how it would have looked
Error correct in the row finds 4 not 3 and colour in cells D22, E23 & F24 Cyan


Book1
ABCDEFGHIJKLMNOP
1
2
3
4
5ROW NC1C2C3C4C5C6C7C8C9C10C11C12C13C14
66555
775555
8855555555
9955555555
10105555555
11115555
121255555
131355555555
14145555
151555555555
161655555555
17175555
181855555
1919555555555
2020555555
2121555555555
22225555555
23235555555
242455555555555
252555555
2626555555
272755555
2828555555555
292955555555
303055555
31315555555
32325555
333355555
34345555555
353555555555
363655555555
37375555555555
383855555555
3939555555
404055555
414155555555
4242555555
Sheet1


It all has happened just overwriting them without following any rule while colouring jut it was to identify the diagonals

I hope I have explained the method how I followed colouring the diagonals

Have a nice day

Good Luck

Regards,
Kishan
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
This is better !!!
Code:
[COLOR=navy]Sub[/COLOR] MG09Sep51
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, Ac [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] col [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] c [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Dim[/COLOR] Tdn [COLOR=navy]As[/COLOR] Range, nDn [COLOR=navy]As[/COLOR] Range, fd [COLOR=navy]As[/COLOR] Boolean
[COLOR=navy]Set[/COLOR] Rng = Range(Range("C6"), Range("C" & Rows.Count).End(xlUp))
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    [COLOR=navy]For[/COLOR] Ac = 0 To 11
        [COLOR=navy]Set[/COLOR] nDn = Union(Dn.Offset(, Ac), Dn.Offset(1, Ac + 1), Dn.Offset(2, Ac + 2))
        [COLOR=navy]Set[/COLOR] Tdn = Union(Dn, Dn.Offset(1, 1), Dn.Offset(2, 2))
        [COLOR=navy]If[/COLOR] Application.CountA(Tdn) = 3 [COLOR=navy]Then[/COLOR]
            [COLOR=navy]If[/COLOR] Dn.Offset(, Ac).Column = 3 [COLOR=navy]Then[/COLOR]
                c = c + 1
                col = IIf(c Mod 2 = 0, vbGreen, vbYellow)
            [COLOR=navy]End[/COLOR] If
            [COLOR=navy]If[/COLOR] Application.CountA(nDn) = 3 [COLOR=navy]Then[/COLOR]
                nDn.Interior.Color = col
            [COLOR=navy]End[/COLOR] If
        [COLOR=navy]End[/COLOR] If
    
    [COLOR=navy]Next[/COLOR] Ac
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
Thank you Mick, for looking in to it again and making it as per request.

It is fine I appreciate your help

Good Luck

Regards
Kishan :)

 
Last edited:
Upvote 0
4-here is the answer of your question finds 3 diagonal in the row 21....
1st-C21, D22, & E23, 2nd-I21, J22, & K23, 3rd-M21, N22, & O23 I did fill these cells with colour yellow.
After that I went to row 22 find 1st-C22, D23 & E24 filled with colour Cyan, find 2nd- H22, I23 & J24 filled with colour Cyan, 3rd- J22, K23 & L24 also fill with colour Cyan overwriting colour cyan yellow despaired. Now I see there is 4th-diagonal which I missed D22, E23 & F24 I would have fill Cyan too in it.
That is what we needed know. Now, I don't know if this will work in your version of Excel (XL2000) as i cannot find information on what version the SpecialCells function was introduced. But if your version does support it, then this macro should do what you want...
Code:
[table="width: 500"]
[tr]
	[td]Sub HighlightDiagonalTriples()
  Dim ColCell As Range, RowCell As Range, TripletColor As Long
  TripletColor = rgbYellow
  For Each ColCell In Range("C6", Cells(Rows.Count, "C").End(xlUp)).SpecialCells(xlConstants)
    If ColCell.Value = ColCell.Offset(1, 1).Value And ColCell.Value = ColCell.Offset(2, 2).Value Then
      For Each RowCell In Range(Cells(ColCell.Row, "C"), Cells(ColCell.Row, "P")).SpecialCells(xlConstants)
        If RowCell.Value = RowCell.Offset(1, 1).Value And RowCell.Value = RowCell.Offset(2, 2).Value Then Union(RowCell, RowCell.Offset(1, 1), RowCell.Offset(2, 2)).Interior.Color = TripletColor
      Next
      TripletColor = rgbYellow + rgbLime - TripletColor
    End If
  Next
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
That is what we needed know. Now, I don't know if this will work in your version of Excel (XL2000) as i cannot find information on what version the SpecialCells function was introduced. But if your version does support it, then this macro should do what you want...
Hi Rick Rothstein, Thank you for the macro, you already had uncertainty about the version will it work or not, it is resulting as shown


Book1
ABCDEFGHIJKLMNOPQRS
1
2
3
4
5ROW NC1C2C3C4C5C6C7C8C9C10C11C12C13C14
66555
775555
8855555555
9955555555
10105555555
11115555
121255555
131355555555
14145555
151555555555
161655555555
17175555
181855555
1919555555555
2020555555
2121555555555
22225555555
23235555555
242455555555555
252555555
2626555555
272755555
2828555555555
292955555555
303055555
31315555555
32325555
333355555
34345555555
Sheet3


Please could you check?

Regards,
Kishan
 
Upvote 0
Hi Rick Rothstein, Thank you for the macro, you already had uncertainty about the version will it work or not, it is resulting as shown

Please could you check?
The fact that it is "coloring" something means the SpecialCells function might be working in your version of Excel (I cannot be sure because the pattern looks a little different than I would be expecting). The reason you are getting all black cells is probably because your version does not have the rgbXXXX color constants defined in it (that must have come in a later version). Here is my code using normal VB color constants instead, see if that works for you...
Code:
[table="width: 500"]
[tr]
	[td]Sub HighlightDiagonalTriples()
  Dim ColCell As Range, RowCell As Range, TripletColor As Long
  TripletColor = vbYellow
  For Each ColCell In Range("C6", Cells(Rows.Count, "C").End(xlUp)).SpecialCells(xlConstants)
    If ColCell.Value = ColCell.Offset(1, 1).Value And ColCell.Value = ColCell.Offset(2, 2).Value Then
      For Each RowCell In Range(Cells(ColCell.Row, "C"), Cells(ColCell.Row, "P")).SpecialCells(xlConstants)
        If RowCell.Value = RowCell.Offset(1, 1).Value And RowCell.Value = RowCell.Offset(2, 2).Value Then Union(RowCell, RowCell.Offset(1, 1), RowCell.Offset(2, 2)).Interior.Color = TripletColor
      Next
      TripletColor = vbYellow + vbGreen - TripletColor
    End If
  Next
End Sub[/td]
[/tr]
[/table]
NOTE: My code is expecting your cells to either be blank or filled with constants (that is, there should be no formulas in any of the cells).
 
Last edited:
Upvote 0
The fact that it is "coloring" something means the SpecialCells function might be working in your version of Excel (I cannot be sure because the pattern looks a little different than I would be expecting). The reason you are getting all black cells is probably because your version does not have the rgbXXXX color constants defined in it (that must have come in a later version). Here is my code using normal VB color constants instead, see if that works for you...
Hi Rick Rothstein, it is better but now but not exactly, as it should be.
Please check the results


Book1
ABCDEFGHIJKLMNOPQRS
1
2
3
4
5ROW NC1C2C3C4C5C6C7C8C9C10C11C12C13C14
66555
775555
8855555555
9955555555
10105555555
11115555
121255555
131355555555
14145555
151555555555
161655555555
17175555
181855555
1919555555555
2020555555
2121555555555
22225555555
23235555555
242455555555555
252555555
2626555555
272755555
2828555555555
292955555555
303055555
31315555555
32325555
333355555
34345555555
353555555555
363655555555
37375555555555
383855555555
3939555555
404055555
414155555555
4242555555
434355555555
44445555555555
45455555
46465555555
47475555555555
4848555555
494955555
505055555
51515555555555
52525555
5353555555555
54545555555
555555555
565655555555
57575555555
5858555555
59595555
60605555555
61615555
626255555555555
6363555555
6464555555
656555555555
66665555555
67675555555
6868555
696955555
7070555555
717155555
7272555555
7373555555
7474555555
757555555555
7676555555
777755
7878555
7979
8080
81
82
83
84
Sheet3


Thank you

Regards,
Kishan
 
Upvote 0
Hi Rick Rothstein, it is better but now but not exactly, as it should be.
Are your blank cells actually blank or could they have maybe space characters in them? You can test this by executing the following line of code in the VB editor's Immediate Window. Using cell G8 because it is colored but has no text in it, execute this line of code there and tell me the result...

? Len(Range("G8").Value)

Also, just verifying... you do not have any formulas in Columns C:P, correct?
 
Last edited:
Upvote 0
Are your blank cells actually blank or could they have maybe space characters in them? You can test this by executing the following line of code in the VB editor's Immediate Window. Using cell G8 because it is colored but has no text in it, execute this line of code there and tell me the result...

? Len(Range("G8").Value)

Also, just verifying... you do not have any formulas in Columns C:P, correct?
Hi Rick Rothstein, I was struggling who to use your formula "Len(Range("G8").Value)" and where to place it and how to get value for the cell G8 this was new for me finally I get it done and find G8 were showing Range ("G8").Value = "" so I done some test and select the range A1:S84 replaced empty cell with 0 and then replaced 0 with nothing and run the code it give the desire results.

Here are the results I think now all is working OK!!


Book1
ABCDEFGHIJKLMNOPQRS
1
2
3
4
5ROW NC1C2C3C4C5C6C7C8C9C10C11C12C13C14
66555
775555
8855555555
9955555555
10105555555
11115555
121255555
131355555555
14145555
151555555555
161655555555
17175555
181855555
1919555555555
2020555555
2121555555555
22225555555
23235555555
242455555555555
252555555
2626555555
272755555
2828555555555
292955555555
303055555
31315555555
32325555
333355555
34345555555
353555555555
363655555555
37375555555555
383855555555
3939555555
404055555
414155555555
4242555555
434355555555
44445555555555
45455555
46465555555
47475555555555
4848555555
494955555
505055555
51515555555555
52525555
5353555555555
54545555555
555555555
565655555555
57575555555
5858555555
59595555
60605555555
61615555
626255555555555
6363555555
6464555555
656555555555
66665555555
67675555555
6868555
696955555
7070555555
717155555
7272555555
7373555555
7474555555
757555555555
7676555555
777755
7878555
7979
8080
81
82
83
84
Sheet3


Thank you so much for your help

Have a nice day

Good Luck

Regards,
Kishan


 
Upvote 0
Hi Rick Rothstein, I was struggling who to use your formula "Len(Range("G8").Value)" and where to place it and how to get value for the cell G8 this was new for me finally I get it done and find G8 were showing Range ("G8").Value = "" so I done some test and select the range A1:S84 replaced empty cell with 0 and then replaced 0 with nothing and run the code it give the desire results.
You were supposed to execute the code line I posted in the VBA editor's Immediate Window, not on the worksheet. And the question mark is part of the code line (it is the shortcut for the Print keyword). No matter, I think you have told me enough to figure out what needs to be done to make my code work on your data. See if this revised code works on the data it previously failed on...
Code:
[table="width: 500"]
[tr]
	[td]Sub HighlightDiagonalTriples()
  Dim ColCell As Range, RowCell As Range, TripletColor As Long
  With Range("C6", Cells(Rows.Count, "C").End(xlUp))
    .Value = .Value
    TripletColor = vbYellow
    For Each ColCell In .SpecialCells(xlConstants)
      If ColCell.Value = ColCell.Offset(1, 1).Value And ColCell.Value = ColCell.Offset(2, 2).Value Then
        For Each RowCell In Range(Cells(ColCell.Row, "C"), Cells(ColCell.Row, "P")).SpecialCells(xlConstants)
          If RowCell.Value = RowCell.Offset(1, 1).Value And RowCell.Value = RowCell.Offset(2, 2).Value Then Union(RowCell, RowCell.Offset(1, 1), RowCell.Offset(2, 2)).Interior.Color = TripletColor
        Next
        TripletColor = vbYellow + vbGreen - TripletColor
      End If
    Next
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
You were supposed to execute the code line I posted in the VBA editor's Immediate Window, not on the worksheet. And the question mark is part of the code line (it is the shortcut for the Print keyword). No matter, I think you have told me enough to figure out what needs to be done to make my code work on your data. See if this revised code works on the data it previously failed on...
Hi Rick Rothstein, Ok I got it how to execute the line code " Len(Range("G8").Value)" in the VBA editor's Immediate Window pointing mouse on the code line I can see this (Range ("G8").Value = "")
I run the new code from the post# 29 no luck it is resulting as shown in the post#26

Thank you

Regards,
Kishan

 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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