Highlights only last rows

motilulla

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

I am looking if someone can make VBA that can highlights last rows in the column C:H as per data last data row column K:Q as per example below


Book1
ABCDEFGHIJKLMNOPQ
1
2
3
4NYearn1n2n3n4n5n6n7EM1N1N2N3N4N5N6N7
5NYearn1n2n3n4n5n6n7EM1N1N2N3N4N5N6N7
6170/71X1121211322541
7270/7121XX2225142214
8370/7121211221135211
9470/711XX111X5521145
10570/71212XX1X2223611
11670/71222111X1612211
12770/71111X2223262544
13870/711X112121412121
14970/7111211X112313108
151070/71221X1224423132
161170/712XXX2121371331
171270/7121X222213111121
181370/7112XX2124312121
191470/71XX1121113345115
201570/7111221122363512
211670/71X2112212322242
221770/71112X11X22242211
231870/7112X11221252123
241970/7121212117221323
252070/71112XX1121131511
262170/71211X2122151213
272270/711X212112823112
282370/7112XX2221552152
292470/71X1X21128319621
302570/71X1111211143123
312670/71122222X3342319
32
33
Sheet10


Thank you all
Excel 2000
Regards,
Moti
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this. I assume that C6:I last row should not have any color except that witch the macro applies. So all color is removed before the new colors are applied.

Code:
Sub hllr()

Dim lr As Long


lr = Cells(Rows.Count, 1).End(xlUp).Row
 
Range("C6:H" & lr).Interior.Color = xlNone
 
Range(Cells(lr - Cells(lr, 11) + 1, 3), Cells(lr, 3)).Interior.ColorIndex = 8
Range(Cells(lr - Cells(lr, 12) + 1, 4), Cells(lr, 4)).Interior.ColorIndex = 8
Range(Cells(lr - Cells(lr, 13) + 1, 5), Cells(lr, 5)).Interior.ColorIndex = 8
Range(Cells(lr - Cells(lr, 14) + 1, 6), Cells(lr, 6)).Interior.ColorIndex = 8
Range(Cells(lr - Cells(lr, 15) + 1, 7), Cells(lr, 7)).Interior.ColorIndex = 8
Range(Cells(lr - Cells(lr, 16) + 1, 8), Cells(lr, 8)).Interior.ColorIndex = 8
Range(Cells(lr - Cells(lr, 17) + 1, 9), Cells(lr, 9)).Interior.ColorIndex = 8


End Sub
 
Upvote 0
Not sure what you mean by "as per data last data row column K:Q" or why your other cells are already filled so as a starter the below highlights the last row from columns C to H, then you can expand on what you actually want.

Code:
Sub lstrwH()
Intersect(Rows(Cells(Rows.Count, "A").End(xlUp).Row), Columns("C:H")).Interior.ColorIndex = 6
End Sub

EDIT: Please ignore the above post as I think having seen Scott T's post he has grasped the question which I hadn't.
 
Last edited:
Upvote 0
Try this. I assume that C6:I last row should not have any color except that witch the macro applies. So all color is removed before the new colors are applied.

Code:
Sub hllr()

Dim lr As Long


lr = Cells(Rows.Count, 1).End(xlUp).Row
 
Range("C6:H" & lr).Interior.Color = xlNone
 
Range(Cells(lr - Cells(lr, 11) + 1, 3), Cells(lr, 3)).Interior.ColorIndex = 8
Range(Cells(lr - Cells(lr, 12) + 1, 4), Cells(lr, 4)).Interior.ColorIndex = 8
Range(Cells(lr - Cells(lr, 13) + 1, 5), Cells(lr, 5)).Interior.ColorIndex = 8
Range(Cells(lr - Cells(lr, 14) + 1, 6), Cells(lr, 6)).Interior.ColorIndex = 8
Range(Cells(lr - Cells(lr, 15) + 1, 7), Cells(lr, 7)).Interior.ColorIndex = 8
Range(Cells(lr - Cells(lr, 16) + 1, 8), Cells(lr, 8)).Interior.ColorIndex = 8
Range(Cells(lr - Cells(lr, 17) + 1, 9), Cells(lr, 9)).Interior.ColorIndex = 8


End Sub
Thank you Scott T, worked perfect! Even better than as accepted

Have a good weekend

Regards,
Moti
 
Last edited:
Upvote 0
Hello,

Please can you help?

I require a modification instead of highlighting the only the last rows, highlight number 6 as per assigned in column "S" and then highlight the L:Q row And then highlights the column cells C:I as per numbers are shown in the row L:Q

Example.


Book1
ABCDEFGHIJKLMNOPQRST
1
2
3
4NYearn1n2n3n4n5n6n7EM1N1N2N3N4N5N6N7EM2
5NYearn1n2n3n4n5n6n7EM1N1N2N3N4N5N6N7EM2
6170/71X11212113225415
7270/7121XX22251422145
8370/71212112211352115
9470/711XX111X55211455
10570/71212XX1X22236717
11670/71222111X16122116
12770/71111X22232625949
13870/711X1121214121214
14970/7111211X11231310810
151070/71221X12244231324
161170/712XXX21213713317
171270/7121X22221311112111
181370/7112XX21243121214
191470/71XX112111334511513
201570/71112211223635126
211670/71X21122123222424
221770/71112X11X2224221111
231870/7112X112212521235
241970/71212121172213237
252070/71112XX112113151115
262170/71211X21221512135
272270/711X2121126231126
282370/7112XX22215521525
292470/71X1X211283196219
302570/71X11112111431234
312670/71122222X33423199
32
Sheet10


Thank you all
Excel 2000
Regards,
Moti
 
Upvote 0
So the contents of the cell no longer matters? Just of column S is 6? What should happen if the number of rows to highlight is greater then the number of rows that is on row 11 if the number of rows to highlight is 9 but there are only 6 rows to highlight.
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG01Nov56
[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] R [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("B6", Range("B" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
[COLOR="Navy"]If[/COLOR] Dn.Offset(, 17).Value = 6 [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]For[/COLOR] ac = 1 To 7
        [COLOR="Navy"]Set[/COLOR] R = Dn.Offset(, ac + 8)
        R.Interior.ColorIndex = 8
        Dn.Offset(-R + 1, ac).Resize(R).Interior.ColorIndex = 8
    [COLOR="Navy"]Next[/COLOR] ac
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
So the contents of the cell no longer matters? Just of column S is 6? What should happen if the number of rows to highlight is greater then the number of rows that is on row 11 if the number of rows to highlight is 9 but there are only 6 rows to highlight.
I didn't thought that yes I think it only can affect if 6 is find in the cells S10, S9, S8, S7 or in the S6 (because 6 is the max number of Row11 (K11:Q11)) in this case we can limit start row 11 to down may be.
 
Upvote 0
Try this:-
Code:
[COLOR=navy]Sub[/COLOR] MG01Nov56
[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] R [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Set[/COLOR] Rng = Range("B6", Range("B" & Rows.Count).End(xlUp))
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
[COLOR=navy]If[/COLOR] Dn.Offset(, 17).Value = 6 [COLOR=navy]Then[/COLOR]
    [COLOR=navy]For[/COLOR] ac = 1 To 7
        [COLOR=navy]Set[/COLOR] R = Dn.Offset(, ac + 8)
        R.Interior.ColorIndex = 8
        Dn.Offset(-R + 1, ac).Resize(R).Interior.ColorIndex = 8
    [COLOR=navy]Next[/COLOR] ac
[COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
Thank you MickG, it is perfect solution as Scott T question not to overlap if 6 find before the B11 I just changed B6 to B11

What if I ask can you make it could be highlighted in 2 alternate colours like cyan and green (I mean to say row 11 in cyan row 20 in green row 22 in cyan may be)

Regards,
Moti
 
Upvote 0
This should only highlight rows 6 and down

Code:
Sub highlight()
Dim lr As Long
Dim cell As Range
Dim myrow As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
Range("C6:I" & lr).Interior.Color = xlNone
For Each cell In Range("S6:S" & lr)
    If cell = 6 Then
        myrow = cell.Row
            If Intersect(Range("C5:I5"), Range(Cells(myrow - Cells(myrow, 11) + 1, 3), Cells(myrow, 3))) Is Nothing Then
                Range(Cells(myrow - Cells(myrow, 11) + 1, 3), Cells(myrow, 3)).Interior.ColorIndex = 8
            Else
                Range(Cells(6, 3), Cells(myrow, 3)).Interior.ColorIndex = 8
            End If
            
            If Intersect(Range("C5:I5"), Range(Cells(myrow - Cells(myrow, 12) + 1, 4), Cells(myrow, 4))) Is Nothing Then
                Range(Cells(myrow - Cells(myrow, 12) + 1, 4), Cells(myrow, 4)).Interior.ColorIndex = 8
            Else
                Range(Cells(6, 4), Cells(myrow, 4)).Interior.ColorIndex = 8
            End If
            
            If Intersect(Range("C5:I5"), Range(Cells(myrow - Cells(myrow, 13) + 1, 5), Cells(myrow, 5))) Is Nothing Then
                Range(Cells(myrow - Cells(myrow, 13) + 1, 5), Cells(myrow, 5)).Interior.ColorIndex = 8
            Else
                Range(Cells(6, 5), Cells(myrow, 5)).Interior.ColorIndex = 8
            End If
            
            If Intersect(Range("C5:I5"), Range(Cells(myrow - Cells(myrow, 14) + 1, 6), Cells(myrow, 6))) Is Nothing Then
                Range(Cells(myrow - Cells(myrow, 14) + 1, 6), Cells(myrow, 6)).Interior.ColorIndex = 8
            Else
                Range(Cells(6, 6), Cells(myrow, 6)).Interior.ColorIndex = 8
            End If
            
            If Intersect(Range("C5:I5"), Range(Cells(myrow - Cells(myrow, 15) + 1, 7), Cells(myrow, 7))) Is Nothing Then
                Range(Cells(myrow - Cells(myrow, 15) + 1, 7), Cells(myrow, 7)).Interior.ColorIndex = 8
            Else
                Range(Cells(6, 7), Cells(myrow, 7)).Interior.ColorIndex = 8
            End If
        
            If Intersect(Range("C5:I5"), Range(Cells(myrow - Cells(myrow, 16) + 1, 8), Cells(myrow, 8))) Is Nothing Then
                Range(Cells(myrow - Cells(myrow, 16) + 1, 8), Cells(myrow, 8)).Interior.ColorIndex = 8
            Else
                Range(Cells(6, 8), Cells(myrow, 8)).Interior.ColorIndex = 8
            End If
            
            If Intersect(Range("C5:I5"), Range(Cells(myrow - Cells(myrow, 17) + 1, 9), Cells(myrow, 9))) Is Nothing Then
                Range(Cells(myrow - Cells(myrow, 17) + 1, 9), Cells(myrow, 9)).Interior.ColorIndex = 8
            Else
                Range(Cells(6, 9), Cells(myrow, 9)).Interior.ColorIndex = 8
            End If
            
    End If

Next cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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