Fill Cells With Colors When A Value Changes

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,501
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I am using the below code to create partition / applying borders in range A4:L4 down till the last non empty cell whenever a value changes in Column B.


Code:
[COLOR=#333333]Sub Test()      [/COLOR]    
Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range
    For Each rng In Range("B4:B" & LastRow)
        If rng <> rng.Offset(1, 0) Then
            Range("A" & rng.Row & ":L" & rng.Row).Borders(xlEdgeBottom).LineStyle = xlContinuous
            Range("A" & rng.Row & ":L" & rng.Row).Borders(xlEdgeBottom).Weight = xlMedium
            
        End If
    Next rng
   [COLOR=#333333]End Sub[/COLOR]


What i want is a code to fill colors in cells where the code is creating partition / applying borders.

For example:

2
2 This partition to turn color 1
2 _______________________
3
3 This partition to turn color 2
3 ______________________
4 ______________________This partition to turn color 1
5
5 This partition to turn color 2
5 ______________________
6
6 This partition to turn color 1
6 ______________________


The data is not constant so i run the code everytime the data changes.
The borders are created with the code mentioned above. I would want the code to fill colors also.
An addition or ammendment in the current code or a new code would do.

Any help would be appreciated.

Regards,
 
Hi,

Could you describe what you exactly have

1. In cell A1

2. In the range A4:A50

The macro you have found relies on these cells to operate ...
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi,

Just wondering ... if you have fix your problem ...???
 
Upvote 0
Hi,

James i don't have access to my computer...

Will get back to you as soon as i am on my computer

Regards,

Humayun
 
Upvote 0
Hi james,

Below is the last code about which i told you that its not working. Like its coloring every other row regardless of the value change.
Code:
[COLOR=#333333]Sub Colorize2()[/COLOR]Dim r As Long, val As Long, c As Long


    r = 1
    val = ActiveSheet.Cells(r, 1).Value
    c = 4                                  '4 is green ,  3 is red '


    For r = 4 To ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
        If ActiveSheet.Cells(r, 2).Value <> val Then
            If c = 3 Then
                c = 4
            Else
                c = 3
            End If
        End If


        ActiveSheet.Range("A" & r & ":L" & r).Select
        With Selection.Interior
            .ColorIndex = c
            .Pattern = xlSolid
        End With
        val = ActiveSheet.Cells(r, 1).Value
    Next r [COLOR=#333333] End Sub[/COLOR]

I just change these part of the code

r = 1 to r=2
val = ActiveSheet.Cells(r, 1).Value to val = ActiveSheet.Cells(r, 2).Value
val = ActiveSheet.Cells(r, 1).Value to val = ActiveSheet.Cells(r, 2).Value

Now the code is working just perfect :)
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,060
Members
449,206
Latest member
Healthydogs

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