How to preserve grid lines while filling color in Excel?
As we all known, when we fill background color to cells, the gridlines will be covered as well, in this case, how could we keep the gridlines while filling background color in Excel cells to get the following screenshot shown.
Preserve gridlines while filling color with VBA code
Preserve gridlines while filling color with VBA code
Normally, there is no direct way for us to solve this job, but, here, I can introduce a VBA code for you. Please do as follows:
1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
2. In the left Project-VBAProject pane, double click ThisWorkbook under the expanded VBAProject, then copy and paste the following VBA code into the blank Module:
VBA code: preserve gridlines while filling color
Dim xRgPre As Range
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
On Error Resume Next
If Not xRgPre Is Nothing Then DrawBorders xRgPre
Set xRgPre = Target
End Sub
Private Sub DrawBorders(ByVal Rg As Range)
'Updateby Extendoffice 20160725
Dim xCell As Range
Application.ScreenUpdating = False
For Each xCell In Rg
If xCell.Interior.ColorIndex = xlNone Then
With xCell.Borders
If .ColorIndex = 15 Then
.LineStyle = xlNone
End If
End With
Else
With xCell.Borders
If .LineStyle = xlNone Then
.Weight = xlThin
.ColorIndex = 15
End If
End With
End If
Next
Application.ScreenUpdating = True
End Sub
3. And then save and close the code window, now, when you fill a color for a range of cells, the gridlines will be showed automatically.
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!