background color change

G2K

Active Member
Joined
May 29, 2009
Messages
355
Hi all,

i have been trying to change cell background color using following code but it's not been working. please help me to fix this problem.


Function ChangeColor(rng As Range, rngcolor As Range)
Dim cell As Range
Dim cellcolor As Long
cellcolor = rngcolor.Range("a1").Interior.ColorIndex
For Each cell In rng
If cell.Interior.Color = vbWhite Then
cell.Interior.ColorIndex = cellcolor
End If
Next cell
End Function


thanks in advance
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
It works fine:

Rich (BB code):
Function ChangeColor(rng As Range, rngcolor As Range)
    Dim cell As Range, cellcolor As Long
    cellcolor = rngcolor.Range("a1").Interior.ColorIndex
    For Each cell In rng
        If cell.Interior.Color = vbWhite Then
            cell.Interior.ColorIndex = cellcolor
        End If
    Next cell
End Function

Sub test()
    ChangeColor [d2:h6], [a2]
End Sub

eb0dd1702a4c.jpg


bca3698d6dd6.jpg
 
Upvote 0
thanks, but i want to call it on excel sheet like UDF

=changecolor(A1:A100,B1)

it's not working over there...
 
Upvote 0
It will not work over there...

UDF cannot change other cells or it's formatting.
(UDF has read-only access to other cells)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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