Macro to change background colurs between two greys but over mutilple lines

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
I need a macro not conditional formation for this please.

I have a range of data in sheet "Cals" Range S11:BX and lastrow.

Now Column S holds the clients name and all clients are sorted to be together.

what I need is a macro that can give me two Greys (pref with RGB numbers so I can edit them easily)
so look down column S and give all the rows with the first client grey, the next client the other grey etc then back to grey .

any ideas?
thank tony
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this
Code:
Sub Grey()
    Dim Cel As Range, Above As Range
    Range("S11").Interior.Color = RGB(150, 150, 150)
    For Each Cel In Range(Range("S12"), Range("S" & Rows.Count).End(xlUp))
        Set Above = Cel.Offset(-1)
        Cel.Interior.Color = Above.Interior.Color
        If Cel <> Above Then
            With Cel.Interior
                If Above.Interior.Color = RGB(150, 150, 150) Then .Color = RGB(205, 205, 205) Else .Color = RGB(150, 150, 150)
            End With
        End If
    Next Cel
End Sub
 
Upvote 0
Above colours column S ony
To apply colour to columns S:BX, amend one line
Code:
With Cel.Interior
With Cel[COLOR=#ff0000].Resize(, 58)[/COLOR].Interior
 
Upvote 0
How about
Code:
Sub tonywatson()
   Dim Cl As Range, Strt As Range
   Dim Clr As String
   
   Clr = RGB(203, 203, 203)
   Set Strt = Range("S11")
   For Each Cl In Range(Range("S12"), Range("S" & Rows.Count).End(xlUp))
      If Cl.Value <> Strt.Value Then
         Range(Strt, Cl.Offset(-1)).Resize(, 58).Interior.Color = Clr
         Set Strt = Cl
         Clr = IIf(Clr = RGB(203, 203, 203), RGB(128, 128, 128), RGB(203, 203, 203))
      End If
   Next Cl
End Sub
 
Upvote 0
Here is how to apply to all columns S:BX
Have included a variable for each colour, which makes it easier to amend as you requested

Code:
Sub Grey()
    Dim Cel As Range, Above As Range, C1, C2
    C1 = RGB(150, 150, 150)
    C2 = RGB(205, 205, 205)
    Range("S11").Resize(, 58).Interior.Color = C1
    For Each Cel In Range(Range("S12"), Range("S" & Rows.Count).End(xlUp))
        Set Above = Cel.Offset(-1)
        Cel.Resize(, 58).Interior.Color = Above.Interior.Color
        If Cel <> Above Then
            With Cel.Resize(, 58).Interior
                If Above.Interior.Color = C1 Then .Color = C2 Else .Color = C1
            End With
        End If
    Next Cel
End Sub

Please ignore post#3
 
Last edited:
Upvote 0
Hi Yongle,
thanks for that last addition it was just what I wanted and have the two colours at the top means I can easily use it for other projects so thank you very much.
Fluff, thanks for your help also, is always good to see alternative ways of writing code as this helps me learn.
Big thanks to both of you for helping me sort this out

Tony
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,546
Members
449,038
Latest member
Guest1337

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