![]() |
Macro to Color Code Cells
Execl 2000:
We have a workbook with a worksheet that can have a varying number of rows. The last 5 rows however will always have data like the following. A B C 51 Total 70 52 53 Test1 20 54 Test2 75 55 Test3 5 We want the macro to compare the number in B53 to B51. If B53 is greated than B51, we want A53 and B53 to be formatted with a green fill color. If B54 is greater than B51, we want A54 and B54 to have a yellow fill. Otherwise we want A55 and B55 to be red. We don't know how to get this to work because the number of rows will always be different when we run the macro. Appreciate any assistance. |
Macro to Color Code Cells
Dim rng as Range
set rng = columns(1).Find("Total") if not rng is nothing then if rng.offset(2,1) rng.offset(0,1) then rng.offset(2,0).Resize(1,2).Interior.ColorIndex = 4 elseif rng.offset(3,1) rng.offset(0,1) then rng.offset(3,0).Resize(1,2).Interior.ColorIndex = 6 else rng.offset(4,0).Resize(1,2).Interior.ColorIndex = 3 End if -- regards, Tom Ogilvy "Ken" wrote in message ... Execl 2000: We have a workbook with a worksheet that can have a varying number of rows. The last 5 rows however will always have data like the following. A B C 51 Total 70 52 53 Test1 20 54 Test2 75 55 Test3 5 We want the macro to compare the number in B53 to B51. If B53 is greated than B51, we want A53 and B53 to be formatted with a green fill color. If B54 is greater than B51, we want A54 and B54 to have a yellow fill. Otherwise we want A55 and B55 to be red. We don't know how to get this to work because the number of rows will always be different when we run the macro. Appreciate any assistance. |
Macro to Color Code Cells
Thanks, Tom. It works perfectly!
(I did have to add a last "End if" line of code.) -----Original Message----- Dim rng as Range set rng = columns(1).Find("Total") if not rng is nothing then if rng.offset(2,1) rng.offset(0,1) then rng.offset(2,0).Resize(1,2).Interior.ColorIndex = 4 elseif rng.offset(3,1) rng.offset(0,1) then rng.offset(3,0).Resize(1,2).Interior.ColorIndex = 6 else rng.offset(4,0).Resize(1,2).Interior.ColorIndex = 3 End if -- regards, Tom Ogilvy "Ken" wrote in message ... Execl 2000: We have a workbook with a worksheet that can have a varying number of rows. The last 5 rows however will always have data like the following. A B C 51 Total 70 52 53 Test1 20 54 Test2 75 55 Test3 5 We want the macro to compare the number in B53 to B51. If B53 is greated than B51, we want A53 and B53 to be formatted with a green fill color. If B54 is greater than B51, we want A54 and B54 to have a yellow fill. Otherwise we want A55 and B55 to be red. We don't know how to get this to work because the number of rows will always be different when we run the macro. Appreciate any assistance. . |
Macro to Color Code Cells
Yes, sorry - didn't close it out.
-- Regards, Tom Ogilvy Ken wrote in message ... Thanks, Tom. It works perfectly! (I did have to add a last "End if" line of code.) -----Original Message----- Dim rng as Range set rng = columns(1).Find("Total") if not rng is nothing then if rng.offset(2,1) rng.offset(0,1) then rng.offset(2,0).Resize(1,2).Interior.ColorIndex = 4 elseif rng.offset(3,1) rng.offset(0,1) then rng.offset(3,0).Resize(1,2).Interior.ColorIndex = 6 else rng.offset(4,0).Resize(1,2).Interior.ColorIndex = 3 End if -- regards, Tom Ogilvy "Ken" wrote in message ... Execl 2000: We have a workbook with a worksheet that can have a varying number of rows. The last 5 rows however will always have data like the following. A B C 51 Total 70 52 53 Test1 20 54 Test2 75 55 Test3 5 We want the macro to compare the number in B53 to B51. If B53 is greated than B51, we want A53 and B53 to be formatted with a green fill color. If B54 is greater than B51, we want A54 and B54 to have a yellow fill. Otherwise we want A55 and B55 to be red. We don't know how to get this to work because the number of rows will always be different when we run the macro. Appreciate any assistance. . |
All times are GMT +1. The time now is 10:49 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com