Colour cells through VBA

motilulla

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

I am looking VBA solution, which can fill the colours if lower line data match with upper line using alternate colourers

For example:
Row3 data match with row2 fill colour red and font white
Row4 data match with row3 fill colour green and font white

I am using conditional format formula =SI(C2=C3;1;0) for row 3 for CF red and white
I am using conditional format formula =SI(C3=C4;1;0) for row 4 for CF green and white and then copy this format to row down

But know I am getting message formatting will not save too much data so I cannot continue formatting more that 1800 rows

So please I need the VBA solution

Sample Data


Book1
BCDEFGHIJKL
1Za1Za1Za1Za1Za1Za1Za1Za1Za1
2A5Y5YAA55
35AYYY5AAY
45A55AAYYA
55A5YY5A55
6A5A5A55A5
7AAA5555A5
8YA555YAAA
9A5AA55555
105AA5Y5Y55
11AYA55A555
12555555AAA
135Y5YA55YA
145AA55YAA5
155AA55Y55Y
16555A5555A
17A5YA555YA
18555A5AA5A
1955555AAY5
20555555555
215YY555555
2255A555555
23AAA555A55
24AY55Y5555
255AYY555YA
26Y5AA55AA5
275A5YY5555
2855A5YYY55
29Y5A5A5A55
30A55555Y5Y
315Y55A555A
32A5555A5Y5
33
Sheet1


Thank you all
Excel 2000
Regards,
Moti
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this:
Code:
Sub Color_Me()
'Modified 1-26-18 12:51 AM EST
Application.ScreenUpdating = False
Dim c As Range
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "C").End(xlUp).Row
    
    For Each c In Range("C2:K" & Lastrow)
        If c.Row Mod 2 = 0 Then If c.Offset(-1, 0).Value = c.Value Then c.Interior.ColorIndex = 4: c.Font.Color = vbWhite
        If c.Row Mod 2 = 1 Then If c.Offset(-1, 0).Value = c.Value Then c.Interior.ColorIndex = 3: c.Font.Color = vbWhite
    Next
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Try this:
Code:
Sub Color_Me()
'Modified 1-26-18 12:51 AM EST
Application.ScreenUpdating = False
Dim c As Range
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "C").End(xlUp).Row
    
    For Each c In Range("C2:K" & Lastrow)
        If c.Row Mod 2 = 0 Then If c.Offset(-1, 0).Value = c.Value Then c.Interior.ColorIndex = 4: c.Font.Color = vbWhite
        If c.Row Mod 2 = 1 Then If c.Offset(-1, 0).Value = c.Value Then c.Interior.ColorIndex = 3: c.Font.Color = vbWhite
    Next
Application.ScreenUpdating = True
End Sub
My Aswer Is This, this is exactly I was looking for, working as request prefect!

Thank you very much for your kind help

Have a good day

Regards,
Moti
 
Upvote 0
You probably do not have enough rows of data to be able to see the time difference, but the following non-looping macro executes in about two-thirds the time of your current macro...
Code:
[table="width: 500"]
[tr]
	[td]Sub ColorIfEqualToAbove()
  Application.ScreenUpdating = False
  Application.ReplaceFormat.Clear
  With Range("C1").CurrentRegion.Offset(1)
    .Resize(.Rows.Count - 1).Resize(.Rows.Count - 1).Value = Evaluate(Replace(Replace("IF(@2=@1,IF(MOD(ROW(@2),2),""|""&@2,""-""&@2),@2)", "@1", .Offset(-1).Address), "@2", .Address))
    Application.ReplaceFormat.Interior.Color = 255
    .Replace "|", "", xlPart, , , , False, True
    .Replace "|", "", xlPart, , , , False, False
    Application.ReplaceFormat.Interior.Color = 32768
    .Replace "-", "", xlPart, , , , False, True
    .Replace "-", "", xlPart, , , , False, False
  End With
  Application.ReplaceFormat.Clear
  Application.ScreenUpdating = False
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Glad I was able to help you. Hope you try reading the script and can learn some from it. You have a nice day also.QUOTE=motilulla;4995405]My Aswer Is This, this is exactly I was looking for, working as request prefect!

Thank you very much for your kind help

Have a good day

Regards,
Moti
[/QUOTE]
 
Upvote 0
You probably do not have enough rows of data to be able to see the time difference, but the following non-looping macro executes in about two-thirds the time of your current macro...
Hello Rick Rothstein, thank you I tried the code and getting following message and highlighted the part of code is shown in red

Complication Error

The number of arguments is incorrect or the property assignment is not valid


Rich (BB code):
.Replace "|", "", xlPart, , , , False, False 


Please could you check?

Regards,
Moti
 
Upvote 0


Rich (BB code):
.Replace "|", "", xlPart, , , , False, False 


Sorry Rick, I just noticed that I copied wrong line

It is the line below one.
Rich (BB code):
 .Replace "|", "", xlPart, , , , False, True

Regards,
Moti
 
Upvote 0
Hello Rick Rothstein, thank you I tried the code and getting following message and highlighted the part of code is shown in red

Complication Error

The number of arguments is incorrect or the property assignment is not valid

Rich (BB code):
.Replace "|", "", xlPart, , , , False, False 


Please could you check?
I tested the code before I posted it and it worked fine then. To be sure, I just tested it again and it still works fine. I am not sure what to tell you as I cannot duplicate the problem you are having. Could you post a copy of your workbook to DropBox so that we can download it and test our code directly against your actual data rather than the made up example we are using now? If you can post it, make sure it has your raw data the way it is before any attempt is made to color it.
 
Upvote 0
In your previous post you said:
"
this is exactly I was looking for, working as request prefect
"

So if the code I provided worked great. Did you now fine a problem with it now?
 
Upvote 0
I tested the code before I posted it and it worked fine then. To be sure, I just tested it again and it still works fine. I am not sure what to tell you as I cannot duplicate the problem you are having. Could you post a copy of your workbook to DropBox so that we can download it and test our code directly against your actual data rather than the made up example we are using now? If you can post it, make sure it has your raw data the way it is before any attempt is made to color it.
Hello Rick, thank you for helping and giving options, I trust it is not a code, as far as I guess it is version problem.

Regards,
Moti
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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