Help needed! to check for presence of cell color

fountainwood

New Member
Joined
Jun 15, 2008
Messages
10
I need help here!
I want to create some code that checks all the cells in a row to find if any cell has a background color and if it finds any colored cell, to then set the first column of that row to that color.

desperate!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,961
Office Version
  1. 365
Platform
  1. Windows
This is for row 1, change as necessary:

Code:
Sub test()
Dim c As Range
For Each c In Range("A1", Cells(1, Columns.Count))
    If c.Interior.ColorIndex <> xlNone Then
        Range("A1").Interior.ColorIndex = c.Interior.ColorIndex
        Exit For
    End If
Next
End Sub
 
Upvote 0

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,961
Office Version
  1. 365
Platform
  1. Windows
I had thought of that, but how you would look for any color, rather than a specific one? That's why I looped.
 
Upvote 0

fountainwood

New Member
Joined
Jun 15, 2008
Messages
10
HotPepper,
'How do I get that to work for all rows in the sheet?
 
Upvote 0

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,961
Office Version
  1. 365
Platform
  1. Windows
If you are on XL2003 or prior, there are 65536 rows, do you really need to check that many, or is there a smaller range you need to check?
 
Upvote 0

Oaktree

MrExcel MVP
Joined
Jun 20, 2002
Messages
8,108
Office Version
  1. 365
Are there specific colors which would be used? If there are say 5 color possibilities, it would save you a lot of time...

What happens if there is more than one colored cell in a row?
 
Upvote 0

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
Code:
Sub Test()
Dim c As Range
For Each c In Sheet1.UsedRange
If c.Interior.ColorIndex <> xlNone Then Cells(c.Row, 1).Interior.ColorIndex = c.Interior.ColorIndex
Next c
End Sub

lenze
 
Upvote 0

Forum statistics

Threads
1,190,857
Messages
5,983,252
Members
439,833
Latest member
CDaviess

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
Top