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
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Doesnt look like i understand the rules. Why isnt G6, H7, I8 highlighted? There are lots more examples of 3 in a diagonal i can see.
 
Upvote 0
Doesnt look like i understand the rules. Why isnt G6, H7, I8 highlighted? There are lots more examples of 3 in a diagonal i can see.
Hi steve the fish,

Your question has logic but rules are if in the column C do not find any diagonal so do not check further diagonal in that row

What I mean C6 is empty so we do not have diagonal in C6, D7, and E8 so forget the row 6 do not track diagonals in the row 6.

Same situation is C7 is empty we do not have diagonal in C7, D8, and E9 so forget the row 7 do not track diagonals in the row 7.

Yes in C8 we have diagonal in C8, D9, and E10 so find further diagonal in the row 8 also find As shown M8, N9, O10

Hope I have explained it.

Thank you

Kishan


 
Upvote 0
Perhaps:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG08Sep08
[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]
[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
    fd = False
    [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] Application.CountA(nDn) = 3 [COLOR="Navy"]Then[/COLOR]
                nDn.Interior.Color = IIf(Dn.Offset(1, Ac).Interior.Color = vbGreen, vbYellow, vbGreen)
            [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
 
Upvote 0
Hi Kishan, not sure it is bullet proof but give this code a try:
Code:
Sub findDiagonals()
    Dim myrange As Range
    Dim mycell As Range
    Dim lastrow As Long
    Dim lastcol As Long
    Dim x As Long
    Dim y As Long
    lastrow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row - 2
    lastcol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column - 2
    Set myrange = Range("c6:n" & lastrow)
    For x = 6 To lastrow
        For y = 3 To lastcol
            If y = 3 And (Cells(x, y).Value = "" Or Cells(x, y).Offset(1, 1).Value <> Cells(x, y).Value Or Cells(x, y).Offset(2, 2).Value <> Cells(x, y).Value) Then
                Exit For
            End If
            If Cells(x, y).Value <> "" [COLOR=#ff0000]And Cells(x, y).Interior.ColorIndex = -4142[/COLOR] And Cells(x, y).Offset(1, 1).Value = Cells(x, y).Value And Cells(x, y).Offset(2, 2).Value = Cells(x, y).Value Then
                If Cells(x, y).Row Mod 2 = 0 Then
                    Cells(x, y).Interior.ColorIndex = 6
                    Cells(x, y).Offset(1, 1).Interior.ColorIndex = 6
                    Cells(x, y).Offset(2, 2).Interior.ColorIndex = 6
                Else
                    Cells(x, y).Interior.ColorIndex = 33
                    Cells(x, y).Offset(1, 1).Interior.ColorIndex = 33
                    Cells(x, y).Offset(2, 2).Interior.ColorIndex = 33
                End If
            End If
        Next y
    Next x
End Sub
If you have issues with diagonals of 4 signs try to remove this part
Code:
And Cells(x, y).Interior.ColorIndex = -4142

Have a nice time
 
Upvote 0
You can do this with Conditional Formatting, although you'll just get one color, not yellow and blue.

In Q6, enter this formula:

=AND(C6<>"",C6=D7,D7=E8)
and drag down to Q80.

Now select the range C6:N78, click Conditional Formatting > New Rule > Use a formula, and enter this formula:
=AND($Q6,C6<>"",C6=D7,D7=E8)

choose a fill color and click OK.

Now select the range D7:O79, and repeat that process with the same formula. Select the range E8:P80 and repeat one last time. You should be good to go.
 
Upvote 0
Perhaps
Regards Mick
Hi Mick, the code is colouring alternate column in 2 different colours yellow & lime but do not highlighting the diagonals.

Please could you check?

Thank you

Kishan
 
Upvote 0
Hi Kishan, not sure it is bullet proof but give this code a try:
Code:
Sub findDiagonals()
    Dim myrange As Range
    Dim mycell As Range
    Dim lastrow As Long
    Dim lastcol As Long
    Dim x As Long
    Dim y As Long
    lastrow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row - 2
    lastcol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column - 2
    Set myrange = Range("c6:n" & lastrow)
    For x = 6 To lastrow
        For y = 3 To lastcol
            If y = 3 And (Cells(x, y).Value = "" Or Cells(x, y).Offset(1, 1).Value <> Cells(x, y).Value Or Cells(x, y).Offset(2, 2).Value <> Cells(x, y).Value) Then
                Exit For
            End If
            If Cells(x, y).Value <> "" [COLOR=#ff0000]And Cells(x, y).Interior.ColorIndex = -4142[/COLOR] And Cells(x, y).Offset(1, 1).Value = Cells(x, y).Value And Cells(x, y).Offset(2, 2).Value = Cells(x, y).Value Then
                If Cells(x, y).Row Mod 2 = 0 Then
                    Cells(x, y).Interior.ColorIndex = 6
                    Cells(x, y).Offset(1, 1).Interior.ColorIndex = 6
                    Cells(x, y).Offset(2, 2).Interior.ColorIndex = 6
                Else
                    Cells(x, y).Interior.ColorIndex = 33
                    Cells(x, y).Offset(1, 1).Interior.ColorIndex = 33
                    Cells(x, y).Offset(2, 2).Interior.ColorIndex = 33
                End If
            End If
        Next y
    Next x
End Sub
If you have issues with diagonals of 4 signs try to remove this part
Code:
And Cells(x, y).Interior.ColorIndex = -4142

Have a nice time
Hi B___P, The first code is working perfect as per request :) does not need to remove part of the line.

Thank you so much for you help

Regards,
Kishan

 
Upvote 0
You can do this with Conditional Formatting, although you'll just get one color, not yellow and blue.

In Q6, enter this formula:

=AND(C6<>"",C6=D7,D7=E8)
and drag down to Q80.

Now select the range C6:N78, click Conditional Formatting > New Rule > Use a formula, and enter this formula:
=AND($Q6,C6<>"",C6=D7,D7=E8)

choose a fill color and click OK.

Now select the range D7:O79, and repeat that process with the same formula. Select the range E8:P80 and repeat one last time. You should be good to go.
Hi Eric W, I tried your Conditional Formatting formula and it is perfect.

Thank you so much for you help

Regards,
Kishan
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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