Macro to apply a border to coloured cells

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Are the cells actually coloured, or is the colour the result of conditional formatting? If the latter, you'll need to set the border as part of the formatting. Otherwise what you need can be done in code.
 
Upvote 0
Hi,

Try

Code:
Sub kTest()
    Dim rngColor    As Range
    Dim i   As Long, j As Long
    
    Set rngColor = Range("b2:h7")
    Application.ScreenUpdating = 0
    With rngColor
        For i = .Rows.Count To 1 Step -1
            For j = .Columns.Count - 1 To 1 Step -1
                If .Cells(i, j).Interior.Color = .Cells(i, j + 1).Interior.Color Then
                    .Cells(i, j).Resize(, 2).Merge
                    .Cells(i, j).MergeArea.BorderAround 1, 3
                Else
                    .Cells(i, j).BorderAround 1, 3
                End If
            Next
        Next
        .UnMerge
        .BorderAround 1, 3
    End With
    Application.ScreenUpdating = 1
End Sub

HTH
 
Last edited:
Upvote 0
try:
Code:
Sub colouredCells()
    Dim r, c, t, i, rng
    Set rng = Range("B2:H7") 'set this to the area your cells are in (doesn't have to be exact)
    Application.ScreenUpdating = 0
    With rng
        For r = 1 To .Rows.Count
            For c = 1 To .Cells.Count
                If .Cells(r, c).Interior.ColorIndex <> xlNone Then
                    t = c
                    Do
                        t = t + 1
                    Loop Until .Cells(r, t).Interior.ColorIndex <> .Cells(r, c).Interior.ColorIndex Or t >= .Cells.Count
                    With .Cells(r, c).Resize(, t - c)
                        For i = 7 To 10
                            With .Borders(i)
                                .LineStyle = xlContinuous
                                .Weight = xlMedium
                            End With
                        Next i
                    End With
                    c = t - 1
                End If
            Next c
        Next r
    End With
            
End Sub
 
Upvote 0
Krishnakumar:
Sorry I didnt explained myself better. Your macro worked great for that specific range of cells. Although... I forgot to mention that there were separate clusters of coloured cells in the same sheet. So I went ahead and tried Weaver's macro which worked exactly the way I needed.

Weaver:
I get a report where the cells are already coloured.
Thank you very much for the help. Your macro worked like a charm.

Alexei
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

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