Highlight only last ones that finds in end of the column

Kishan

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

In the cells C3:K3 got 9 patterns with 3 different colour groups I need a VBA which Highlights only 9 patterns finds last in the column C as shown in the example with same format as C3:K3

Example....


Book1
ABCDEFGHIJKLM
1
2
31|11|X1|2X|1X|XX|22|12|X2|2
4
5
61 | 2
72 | 2
8X | 1
92 | 1
102 | 1
111 | 2
122 | 1
13X | X
142 | 1
151 | 1
161 | 2
171 | 1
18X | X
191 | 1
20X | 1
211 | X
22X | 2
232 | X
241 | 1
25X | 2
262 | 2
27X | X
282 | 1
291 | 2
301 | X
31X | 2
321 | 2
33X | 1
342 | X
35X | 1
36X | X
372 | 2
38
39
40
41
Sheet10


Thank you in advance

Regards,
Kishan
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
.. even simpler:
Code:
Sub Highlight_Last_v2()
  Dim c As Range, cFound As Range
  
  For Each c In Range("C3:K3")
    Set cFound = Range("C6:C37").Find(What:=c.Value, SearchDirection:=xlPrevious)
    If Not cFound Is Nothing Then c.Copy cFound
  Next c
End Sub

.. and if we know that all the values in C3:K3 will exist at least once in C6:C37 then
Code:
Sub Highlight_Last_v3()
  Dim c As Range
  
  For Each c In Range("C3:K3")
    c.Copy Range("C6:C37").Find(What:=c.Value, SearchDirection:=xlPrevious)
  Next c
End Sub
 
Upvote 0
.. even simpler:
Code:
Sub Highlight_Last_v2()
  Dim c As Range, cFound As Range
  
  For Each c In Range("C3:K3")
    Set cFound = Range("C6:C37").Find(What:=c.Value, SearchDirection:=xlPrevious)
    If Not cFound Is Nothing Then c.Copy cFound
  Next c
End Sub

.. and if we know that all the values in C3:K3 will exist at least once in C6:C37 then
Code:
Sub Highlight_Last_v3()
  Dim c As Range
  
  For Each c In Range("C3:K3")
    c.Copy Range("C6:C37").Find(What:=c.Value, SearchDirection:=xlPrevious)
  Next c
End Sub
Peter_SSs, Sub Highlight_Last_v3 is ideal because yes all the values in C3:K3 exist at least once in C6:C37 thank you for shooting the code it is resulting perfect.</SPAN></SPAN>

Can I ask you one more favour? I have add one more column to right side and I want if it could be coloured as shown below in the given example
</SPAN></SPAN>


Book1
ABCDEFGHIJKLM
1
2
31 | 11 | X1 | 2X | 1X | XX | 22 | 12 | X2 | 2
4
5
61 | 25
72 | 20
8X | 12
92 | 19
102 | 19
111 | 25
122 | 19
13X | X1
142 | 19
151 | 113
161 | 25
171 | 113
18X | X1
191 | 113
20X | 12
211 | X7
22X | 26
232 | X3
241 | 113
25X | 26
262 | 20
27X | X1
282 | 19
291 | 25
301 | X7
31X | 26
321 | 25
33X | 12
342 | X3
35X | 12
36X | X1
372 | 20
38
39
40
41
Sheet10-1


Kind Regards
</SPAN></SPAN>
Kishan
</SPAN></SPAN>
 
Last edited:
Upvote 0
I have add one more column to right side and I want if it could be coloured as shown below in the given example
Try
Code:
Sub Highlight_Last_v4()
  Dim c As Range, cFound As Range
  
  For Each c In Range("C3:K3")
    Set cFound = Range("C6:C37").Find(What:=c.Value, SearchDirection:=xlPrevious)
    c.Copy cFound
    cFound.Offset(, 1).Interior.Color = vbGreen
  Next c
End Sub
 
Upvote 0
What about this code?
Code:
Option Explicit


Sub Colorize()
Dim r%, My_color%, Mn%, Mx%
Dim x%, y%
Mn = 3: Mx = 11
r = Range("C6").End(4).Row
Range("C6:D" & r).Interior.ColorIndex = xlNone
For x = Mn To Mx
 
 My_color = Cells(2, x).Interior.ColorIndex
    For y = r To 6 Step -1
       If Cells(y, 3) = Cells(2, x) Then
            With Cells(y, 3)
                .Interior.ColorIndex = My_color
                .Offset(, 1).Interior.Color = vbGreen
            End With
          Exit For
       End If
    Next y
Next x


End Sub
 
Upvote 0
Try
Code:
Sub Highlight_Last_v4()
  Dim c As Range, cFound As Range
  
  For Each c In Range("C3:K3")
    Set cFound = Range("C6:C37").Find(What:=c.Value, SearchDirection:=xlPrevious)
    c.Copy cFound
    cFound.Offset(, 1).Interior.Color = vbGreen
  Next c
End Sub
Peter_SSs, superb! Worked as request. Thank you so much</SPAN></SPAN>

Kind Regards
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
 
Upvote 0
What about this code?
Code:
Option Explicit

Sub Colorize()
  My_color = Cells(2, x).Interior.ColorIndex
    For y = r To 6 Step -1
       If Cells(y, 3) = Cells(2, x) Then
End Sub
salim hasan, thank you it worked but Peter_SSs, code is easy to manage...also I change 2 to 3 following to get work your code with my data layout.</SPAN></SPAN>

Code:
My_color = Cells(3, x).Interior.ColorIndex
    For y = r To 6 Step -1
       If Cells(y, 3) = Cells(3, x) Then

Kind Regards
</SPAN></SPAN>
Kishan
</SPAN></SPAN>
 
Upvote 0
Peter_SSs, superb! Worked as request. Thank you so much</SPAN></SPAN>

Kind Regards
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
You're welcome.



..also I change 2 to 3 following to get work your code with my data layout.
You would have also needed to include the change to white font in that code.
 
Upvote 0
You're welcome.



You would have also needed to include the change to white font in that code.
Peter_SSs, yes I did not bother to change the font colours, as I am using your code it is vary simple and flexible while changing the locations is easy and so practical </SPAN></SPAN>

Thank you for the nice code :)
</SPAN></SPAN>

Kind Regards
</SPAN></SPAN>
Kishan
</SPAN></SPAN>
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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