Apply White Borders to Excel Cells with a Specific Fill Color
Question details
Apply thin white borders to Excel cells that have a specified 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.
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.
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.
In desktop Excel, open the workbook, then select Developer > Visual Basic. If Developer is not visible, enable it in Excel's ribbon settings.
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
Return to the worksheet and select the range to check. Choose Developer > Macros, select WhiteBordersForFill, and click Run.
Use the Data > Sort command on the full data range, then inspect the borders. If needed, select the range and run the macro again.

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.

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.




