Please fix my macro quickly!

ClimoC

Well-known Member
Joined
Aug 21, 2009
Messages
584
Hello

I have a range of data which I want colour-coded if the date in a cell is different to the one above it.

If it is the same as the date above it, I want the colour to be the same as that cell.

The below macro almost works - it scans the range, and if the cell above it is different to its date, the cell changes colour. However, the next cell beneath that (which might happen to share the same date) doesn't change.

Also if possible I'd like to be colouring all rows but only in the array G6:L500 and not the columns outside this array.

here is my code

Code:
Sub colourme()
Dim r As Range
Dim c As Range
Dim rw As Range

Set r = Range("Schedule!G6:G500")
Set rw = r.Rows

        For Each rw In r
           
            If rw.Offset(-1, 1).Value = rw.Offset(0, 1).Value Then
            rw.Interior.ColorIndex = rw.Offset(-1, 1).Interior.ColorIndex
            ElseIf rw.Offset(-1, 1).Value <> rw.Offset(0, 1).Value Then
            rw.Interior.ColorIndex = 7
            End If
        Next
        
End Sub

ANy help would be MUCH appreciated.
Cheers
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi
try these codes
Code:
sub climo()
Dim a As Long, c As Long
c = 2
    For a = 2 To Range("G65536").End(xlUp).Row
    Range(Cells(a, 7), Cells(a, 12)).Interior.ColorIndex = c
        If Cells(a, 7) <> Cells(a + 1, 7) Then
        c = c + 1
        End If
    Next a
MsgBox "complete"
End Sub
ravi
 
Upvote 0
I would try the following:

Use a helper column - in this example column "B" - to check if a row is the same as the row(s) below / above:

Excel Workbook
AB
21.1.20091
31.2.20092
41.1.20093
51.3.20094
61.1.20095
71.1.20095
82.1.20096
92.1.20096
102.1.20096
112.1.20096
123.1.20097
133.1.20097
143.1.20097
153.1.20097
163.1.20097
Tabelle1


in B2 copied down:
Code:
=IF(AND(A2=A1,A2=A3),B1,IF(A2=A1,B1,B1+1))

Then run the following macro:

Sub foo()
Dim cll As Range
For Each cll In Sheets(1).Range(Cells(2, "B"), Cells(16, "B"))
cll.EntireRow.Interior.ColorIndex = cll.Value + 1
Next cll
End Sub

The result:

Excel Workbook
AB
21.1.20091
31.2.20092
41.1.20093
51.3.20094
61.1.20095
71.1.20095
82.1.20096
92.1.20096
102.1.20096
112.1.20096
123.1.20097
133.1.20097
143.1.20097
153.1.20097
163.1.20097
Tabelle1


This gives you an individual color for each new block of dates.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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