changing cell color in named range...

sadams1

Board Regular
Joined
Aug 19, 2006
Messages
219
hello & thank you for considering this insanity I'm looking to do!

In the screenshots...
#1 shows a "named range" I'd like to have the rows filled with a light gray.
#2 shows random colors that could be in any of those cells in the named range
#3 shows the result I'd like to see which is to fill any cell with "no fill" to light gray


I have the following code which tests each cell in a range to see if it's gray & if so, change to "no fill"
Would it be possible to pass a "named range" into this code to accomplish #3 from above.

Many thanks for your time!
Steve




Sub TestFillColor()

Dim cell As Range

'Loop through each cell in selected range
For Each cell In Selection.Cells

'Test if cell has a fill color
If cell.Interior.Color = RGB(242, 242, 242) Then

'change to no fill
cell.Interior.Color = RGB(255, 255, 255)

End If

Next cell

End Sub
 

Attachments

  • Untitled.png
    Untitled.png
    22 KB · Views: 9
  • Untitled2.png
    Untitled2.png
    21.2 KB · Views: 9
  • Untitled3.png
    Untitled3.png
    21.3 KB · Views: 9

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
here are two approaches I'm messing with & getting errors under the "Loop through each cell in selected range" comment...the 1st specifies the range & the 2nd specifies the "named range".

1. For Each cell In Selection.Cells("A5:NM5")

2.
Range("ZZZ").Select
Application.Goto "ZZZ"


It looks like this either would work but something is wrong...any insights would be great...thanks!
 
Upvote 0
I hacked away & came up with the clunky thing but it works!

***********this is 1 of 2...the range is a contiguous and must be defined***

Sub RemoveGray()

Dim cell As Range

Range("ZZZ").Select
Application.Goto "ZZZ"


'Loop through each cell in selected range
For Each cell In Selection.Cells

'Test if cell has a fill color
If cell.Interior.Color = RGB(242, 242, 242) Then
'255 = white

'change to no fill
cell.Interior.Color = RGB(255, 255, 255)
'242 = gray


End If

Next cell

End Sub




************this is 2 of 2...the range is non-contiguous & creates the "ledger paper" alternating colored rows
Sub AddAlternatingGrayRows()

Dim cell As Range

Range("BBB").Select
Application.Goto "BBB"




'Loop through each cell in selected range
For Each cell In Selection.Cells



'Test if cell has a fill color
If cell.Interior.Color = RGB(255, 255, 255) Then
'255 = white

'change to no fill
cell.Interior.Color = RGB(242, 242, 242)
'242 = gray


End If

Next cell

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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