Highlights only last rows if character match

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,362
Office Version
  1. 2010
Hello,

I am looking if someone can make VBA that can highlights last 3 rows in the column C:I if finds the sequence of "1X2" order does not matter must be all 3 character there
As per example below


Book1
ABCDEFGHIJ
1
2
3
4
5
6
7NYearn1n2n3n4n5n6n7
8NYearn1n2n3n4n5n6n7
9170/71X112121
10270/7121XX222
11370/712121122
12470/711XX111X
13570/71212XX1X
14670/71222111X
15770/71111X222
16870/711X11212
17970/7111211X1
181070/71221X122
191170/712XXX212
201270/7121X2222
211370/7112XX212
221470/71XX11211
231570/711122112
241670/71X211221
251770/71112X11X
261870/7112X1122
271970/712121211
282070/71112XX11
292170/71211X212
302270/711X21211
312370/7112XX222
322470/71X1X2112
332570/712111X21
342670/71122222X
35
Sheet2


Thank you all
Excel 2000
Regards,
Moti
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try

Code:
Sub hl1x2()

Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
Range("C9:I" & lr).Interior.ColorIndex = xlNone

For x = 3 To 9
    If IsNumeric(Application.Match(1, Range(Cells(lr - 2, x), Cells(lr, x)), 0)) And IsNumeric(Application.Match("X", Range(Cells(lr - 2, x), Cells(lr, x)), 0)) And IsNumeric(Application.Match(2, Range(Cells(lr - 2, x), Cells(lr, x)), 0)) Then
        Range(Cells(lr - 2, x), Cells(lr, x)).Interior.ColorIndex = 8
    
    End If
Next x

End Sub
 
Upvote 0
Sub Highlight()
Set rg = Range("C9").CurrentRegion
l = rg.Rows.Count + rg.Row - 3
For cl = 3 To 9
If yes(l, cl) Then
Cells(l, cl).Resize(3).Interior.Color = vbGreen
GoTo Nx
End If
Nx:
Next
End Sub
Function yes(rw, col) As Boolean
tot = 0

For i = rw To rw + 2
Select Case Cells(i, col).Value
Case 1
tot = tot + 1
Case "x"
tot = tot + 2
Case 2
tot = tot + 4
End Select
Next
If tot = 7 Then yes = True
End Function
 
Upvote 0
Try

Code:
Sub hl1x2()

Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
Range("C9:I" & lr).Interior.ColorIndex = xlNone

For x = 3 To 9
    If IsNumeric(Application.Match(1, Range(Cells(lr - 2, x), Cells(lr, x)), 0)) And IsNumeric(Application.Match("X", Range(Cells(lr - 2, x), Cells(lr, x)), 0)) And IsNumeric(Application.Match(2, Range(Cells(lr - 2, x), Cells(lr, x)), 0)) Then
        Range(Cells(lr - 2, x), Cells(lr, x)).Interior.ColorIndex = 8
    
    End If
Next x

End Sub
Thank you Scott T, it is running perfect as request.

Code:
Dim lr As Long, x As Long

Good luck!

Regards,
Moti
 
Upvote 0
Code:
    sRow = Cells(Rows.Count, 1).End(xlUp).Row - 2
        For Each c In Range("C" & sRow & ":I" & sRow)
            If c <> c.Offset(1) And c <> c.Offset(2) And c.Offset(1) <> c.Offset(2) Then
                c.Resize(3).Interior.ColorIndex = 8
            End If
        Next
 
Last edited:
Upvote 0
Sub Highlight()
Set rg = Range("C9").CurrentRegion
l = rg.Rows.Count + rg.Row - 3
For cl = 3 To 9
If yes(l, cl) Then
Cells(l, cl).Resize(3).Interior.Color = vbGreen
GoTo Nx
End If
Nx:
Next
End Sub
Function yes(rw, col) As Boolean
tot = 0

For i = rw To rw + 2
Select Case Cells(i, col).Value
Case 1
tot = tot + 1
Case "x"
tot = tot + 2
Case 2
tot = tot + 4
End Select
Next
If tot = 7 Then yes = True
End Function
Thank you BobUmlas, I checked your code it is not highlighting any cells I always suspect it is because of version 2000.

Good luck!

Regards,
Moti
 
Upvote 0
Code:
    sRow = Cells(Rows.Count, 1).End(xlUp).Row - 2
        For Each c In Range("C" & sRow & ":I" & sRow)
            If c <> c.Offset(1) And c <> c.Offset(2) And c.Offset(1) <> c.Offset(2) Then
                c.Resize(3).Interior.ColorIndex = 8
            End If
        Next
Thank you sericom, it is running perfect.

Added following 3 Scott T lines
Rich (BB code):
 Sub ABC()
Dim lr As Long, x As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
Range("C9:I" & lr).Interior.ColorIndex = xlNone
 
sRow = Cells(Rows.Count, 1).End(xlUp).Row - 2
        For Each c In Range("C" & sRow & ":I" & sRow)
            If c <> c.Offset(1) And c <> c.Offset(2) And c.Offset(1) <> c.Offset(2) Then
                c.Resize(3).Interior.ColorIndex = 8
            End If
        Next
End Sub

Good luck!

Regards,
Moti
 
Upvote 0
No need to work out the last row twice

Code:
Sub ABC()
    Dim lr As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Range("C9:I" & lr).Interior.ColorIndex = xlNone
    For Each c In Range("C" & lr - 2 & ":I" & lr - 2)
        If c <> c.Offset(1) And c <> c.Offset(2) And c.Offset(1) <> c.Offset(2) Then
            c.Resize(3).Interior.ColorIndex = 8
        End If
    Next
End Sub
 
Last edited:
Upvote 0
No need to work out the last row twice

Code:
Sub ABC()
    Dim lr As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Range("C9:I" & lr).Interior.ColorIndex = xlNone
    For Each c In Range("C" & lr - 2 & ":I" & lr - 2)
        If c <> c.Offset(1) And c <> c.Offset(2) And c.Offset(1) <> c.Offset(2) Then
            c.Resize(3).Interior.ColorIndex = 8
        End If
    Next
End Sub
Thank you sericom, this did the trick now it is perfect!

Good luck!

Regards,
Moti

 
Upvote 0

Forum statistics

Threads
1,215,504
Messages
6,125,183
Members
449,212
Latest member
kenmaldonado

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