Formatting Issues

Apply White Borders to Excel Cells with a Specific Fill Color

John WilsonJohn Wilson Nov 22, 2026 868 views

Question details

Apply thin white borders to Excel cells that have a specified fill color.

Apply White Borders to Excel Cells with a Specific Fill Color
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user wants to identify cells by fill color and format their borders, including when working with sorted data.
Observed behavior
Conditional formatting can change the displayed background color during sorting, but may not preserve the desired borders.
Before you start

Use a desktop version of Excel that supports VBA and identify the cells to format. The example matches the stored fill color RGB(102, 204, 205); a color displayed by conditional formatting may need a different check.

Solution 1Recommended

Use a VBA macro to border cells with the target fill

Apply thin white borders to selected cells whose stored fill color matches the specified RGB value.

1
Open the VBA editor

In desktop Excel, open the workbook, then select Developer > Visual Basic. If Developer is not visible, enable it in Excel's ribbon settings.

2
Insert the macro

In the VBA editor, select Insert > Module and paste this code: Sub WhiteBordersForFill() Dim cell As Range Dim edge As Variant For Each cell In Selection.Cells If cell.Interior.Color = RGB(102, 204, 205) Then cell.Interior.Pattern = xlSolid cell.Borders(xlDiagonalDown).LineStyle = xlNone cell.Borders(xlDiagonalUp).LineStyle = xlNone For Each edge In Array(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight) With cell.Borders(edge) .LineStyle = xlContinuous .Weight = xlThin .Color = RGB(255, 255, 255) End With Next edge End If Next cell End Sub

3
Select cells and run the macro

Return to the worksheet and select the range to check. Choose Developer > Macros, select WhiteBordersForFill, and click Run.

4
Check the result after sorting

Use the Data > Sort command on the full data range, then inspect the borders. If needed, select the range and run the macro again.

Use a VBA macro to border cells with the target fill
Conditional formatting and sorting: Interior.Color checks the stored fill, not necessarily the color displayed by a conditional formatting rule. To check the displayed color instead, replace cell.Interior.Color with cell.DisplayFormat.Interior.Color in the If statement. Conditional formatting can still override the visible border, and sorting behavior depends on the range and formatting rules.
Free Microsoft Office alternative

Format spreadsheet cells with WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and use cell formatting tools as you move between office suites; it does not fix Excel's VBA or sorting behavior.

Apply cell fills and borders using spreadsheet formatting toolsOpen and work with Microsoft Excel XLSX filesUse a familiar interface for an easier transition from Microsoft OfficeWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

How do I change which fill color the macro matches?

In Developer > Visual Basic, edit RGB(102, 204, 205) in the If statement to use the red, green, and blue values of the fill you want.

Why did the macro skip a cell that looks like the target color?

The visible color may come from conditional formatting. In the VBA editor, use cell.DisplayFormat.Interior.Color instead of cell.Interior.Color to test the displayed fill.

Will the borders always follow my data when I sort?

Not necessarily. Sorting and conditional formatting rules can affect the result. Sort the full data range with Data > Sort, inspect the cells, and rerun the macro if needed.

How do I keep the macro in the workbook?

Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). Follow Excel's security prompts when opening and running macros from a trusted workbook.