VBA code required: Select all cells in the worksheet that are shaded the same colour as the active cell

NicoMellett

New Member
Joined
Nov 25, 2008
Messages
8
I have been Googling for ages, but for some reason I can't find a simple solution to this problem. All I want to do is run a macro, and Excel selects the cells in the current worksheet that are shaded the same colour as the current cell.

P.S. No need to consider Conditional Formatting.

Thanks in advance!

Cheers
Nico
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try this
Rich (BB code):
Sub SelectCells1()      'Includes active cell
    Dim Kolor As Long, Cel As Range, Rng As Range
    Kolor = activeCell.DisplayFormat.Interior.Color
    Set Rng = activeCell
    For Each Cel In ActiveSheet.UsedRange
        If Cel.DisplayFormat.Interior.Color = Kolor Then Set Rng = Union(Rng, Cel)
    Next Cel
    If Not Rng Is Nothing Then Rng.Select
End Sub

Sub SelectCells2()      'Excludes active cell
    Dim Kolor As Long, Cel As Range, Rng As Range
    Kolor = activeCell.DisplayFormat.Interior.Color
    For Each Cel In ActiveSheet.UsedRange
        If Not Cel.Address = activeCell.Address Then
            If Cel.DisplayFormat.Interior.Color = Kolor Then
                If Not Rng Is Nothing Then Set Rng = Union(Rng, Cel) Else Set Rng = Cel
            End If
        End If
    Next Cel
    If Not Rng Is Nothing Then Rng.Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,594
Messages
6,120,436
Members
448,964
Latest member
Danni317

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