Getting Value of a coloured cell(vba)

excel.vba

New Member
Joined
May 26, 2011
Messages
30
Hello

I am currently working on a project that requires me to send over the value of a coloured cell from one sheet to another. The code needs to search the range for that particular colour and send over the value in that coloured cell

Here is my code on that:
Public Function ColourValue(rnArea As Range, ColIndex As Long) As Range
Dim rnCell As Range, rRange As Range
For Each rnCell In rnArea
If rnCell.Interior.ColorIndex = ColIndex Then
ColourCOL = rnCell
End If
Next rnCell
End Function

Would gladly appreciate any help. Thank you
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
And what is the question, anyway? What is variable "ColourCOL"?? Where it's used?
I guess this:

Code:
Public Function ColourValue(rnArea As Range, ColIndex As Long) As Range
    Dim cell As Range, rngUnion As Range
    For Each cell In rnArea
        If rnCell.Interior.ColorIndex = ColIndex Then
            [COLOR="Red"][COLOR="#ff0000"]Set rngUnion[/COLOR] = Union(rngUnion, cell)[/COLOR]
        End If
    Next rnCell
    [COLOR="Red"]Set ColourValue = rngUnion[/COLOR]
End Function
 
Last edited:
Upvote 0
Oops sorry! Forgot to change that. The code is not working.It returns #Value when i use this function.

Public Function ColourValue(rnArea As Range, ColIndex As Long) As Range
Dim rnCell As Range, rRange As Range
For Each rnCell In rnArea
If rnCell.Interior.ColorIndex = ColIndex Then
ColourValue = rnCell
End If
Next rnCell
End Function
 
Upvote 0
Here's a different version of the same approach.
Code:
Function CellsHoldingColor(rangeToSearch As Range, colorIndexSought As Long) As Range
    Dim oneCell As Range
    On Error GoTo ErrorRoutine
    
    For Each oneCell In rangeToSearch
        With oneCell
            If .Interior.ColorIndex = colorIndexSought Then
                Set CellsHoldingColor = Application.Union(CellsHoldingColor, .Cells)
            End If
        End With
    Next oneCell
    
ErrorRoutine:
    If Err = 5 Then
        Set CellsHoldingColor = oneCell
        Resume
    End If
    On Error GoTo 0
End Function

Sub test()
    Dim coloredCells As Range
    
    Set coloredCells = CellsHoldingColor(Range("A1:a10"), 6)
    
    If coloredCells Is Nothing Then
        MsgBox "no yellow cells"
    Else
        MsgBox coloredCells.Address & " are yellow."
    End If
End Sub

Odd observation:

As written, test returned "$A$2,$A$4,$A$8 are yellow."

But if the order of the arguments of Union were switched
Code:
Set CellsHoldingColor = Application.Union(.Cells, CellsHoldingColor)
Then "$A$8:$A$9,$A$4,$A$2 are yellow." was returned. The Areas of the range were in different order.
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,921
Latest member
BBQKING

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