Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

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.

doc keep gridlines fill color 1

Preserve gridlines while filling color with VBA code


arrow blue right bubble 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

doc keep gridlines fill color 2

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.


Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial.
kte tab 201905

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!
officetab bottom
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Alissa · 1 years ago
    Another fix, if you don't want to mess with code:
    First figure out which color gray matches the default lines and have it in mind.
    In the Home tab click Cell styles, right click on Normal, click Modify -> Format -> Border. For Line Color, select that gray. For Presets, select Outline. Done.
    Now you may have to select all (CTRL+A / Command+A) and click Cell styles -> Normal to make sure it's applied to all cells. When you fill a cell, the borders will remain.
  • To post as a guest, your comment is unpublished.
    Andrew · 1 years ago
    Cool, but now if I fill some cells, and hit Ctrl+Z to undo, it won't work. Will not undo.
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hello, Andrew,
      Yes, the above code can not support Undo feature, you should fill no color manually to undo your work.
      Thank you!
  • To post as a guest, your comment is unpublished.
    barbatus · 1 years ago
    use only standard colors ;)
  • To post as a guest, your comment is unpublished.
    Terry · 1 years ago
    works but one has to correct the error if you see it.
    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


    *************************************
    Should READ... as the If statement only ensures that something is there to set to memory allocation.

    One could set the next line below for clean up and not allow memory leaks.

    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    On Error Resume Next
    Set xRgPre = Target
    If Not xRgPre Is Nothing Then DrawBorders xRgPre
    'For ensured memory leaks do not happen
    'Clean up the memory allocated after it is not needed.
    Set xRqPre = Nothing



    Have a nice day..
  • To post as a guest, your comment is unpublished.
    Vic · 2 years ago
    Works fine, but sadly every time I now click on a cell, the worksheet judders! Removed the VBA and the judder has ceased. Wonder why this should happen?
    • To post as a guest, your comment is unpublished.
      Jeremy Jones · 5 months ago
      Same here with Excel 2016. I'm getting freezing and mostly blanked out screen for several seconds, especially when selecting many cells. It's a shame because it does work perfectly otherwise.
    • To post as a guest, your comment is unpublished.
      skyyang · 2 years ago
      Hi, Vic,
      There is no this problem in my Excel workbook, which Excel version do you use?
  • To post as a guest, your comment is unpublished.
    dolphinks · 2 years ago
    Fabulous fix!!