Note: The other languages of the website are Google-translated. Back to English
登入  \/ 
x
or
x
註冊  \/ 
x

or

在Excel中填充顏色時如何保留網格線?

眾所周知,當我們為單元格填充背景色時,網格線也會被覆蓋,在這種情況下,如何在填充Excel單元格中的背景色時保持網格線以顯示以下屏幕截圖。

doc保持網格線填充顏色1

保留網格線,同時使用VBA代碼填充顏色


箭頭藍色右氣泡 保留網格線,同時使用VBA代碼填充顏色


通常,沒有直接的方法可以讓我們解決這個問題,但是,在這裡,我可以為您介紹一個VBA代碼。 請執行以下操作:

1。 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

2。 在左邊 項目-VBA項目 窗格,雙擊 的ThisWorkbook 在擴展下 VBA項目,然後將以下VBA代碼複製並粘貼到空白模塊中:

VBA代碼:填充顏色時保留網格線

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保持網格線填充顏色2

3。 然後保存並關閉代碼窗口,現在,當您為一系列單元格填充顏色時,網格線將自動顯示。


最佳辦公效率工具

Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%

  • 重用: 快速插入 複雜的公式,圖表 以及您以前使用過的任何東西; 加密單元 帶密碼 創建郵件列表 並發送電子郵件...
  • 超級公式欄 (輕鬆編輯多行文本和公式); 閱讀版式 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
  • 合併單元格/行/列 不會丟失數據; 拆分單元格內容; 合併重複的行/列...防止細胞重複; 比較範圍...
  • 選擇重複或唯一 行; 選擇空白行 (所有單元格都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇...
  • 確切的副本 多個單元格,無需更改公式參考; 自動創建參考 到多張紙; 插入項目符號,複選框等...
  • 提取文字,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
  • 超級濾鏡 (將過濾方案保存並應用於其他工作表); 高級排序 按月/週/日,頻率及更多; 特殊過濾器 用粗體,斜體...
  • 結合工作簿和工作表; 根據關鍵列合併表; 將數據分割成多個工作表; 批量轉換xls,xlsx和PDF...
  • 超過300種強大功能。 支持Office / Excel 2007-2019和365。支持所有語言。 在您的企業或組織中輕鬆部署。 完整功能30天免費試用。 60天退款保證。
kte選項卡201905

Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 每天將您的工作效率提高50%,並減少數百次鼠標單擊!
officetab底部
Say something here...
symbols left.
You are guest
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.
    sculbertson1106 · 2 months ago
    This works great. (Thanks)  But here's the real question - Why does Excel allow gridlines to disappear when filling cells with color in the first place? Just another dumb glitch in the techy world that so-called intelligent developers never think of. I mean, the gridlines, by default are supposed to be there anyway...so....
  • To post as a guest, your comment is unpublished.
    Chad · 1 years ago
    Thank you! I was pulling hairs trying to get the nice grey-ish borders to stay after colorizing. Can't believe Microsoft hasn't already implemented this.
  • To post as a guest, your comment is unpublished.
    Alissa · 2 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.
      JL · 9 months ago
      This helped me SO much. I have literally been searching so many places and this is the only explanation that worked! Thank you!!!
    • To post as a guest, your comment is unpublished.
      Fox · 1 years ago
      Alissa , I had the same problem couldn`t get it fixed but your reply helped me a lot , so thank you very much , you are genius .
    • To post as a guest, your comment is unpublished.
      Armaan Sandhu · 2 years ago
      Thank you!!
  • To post as a guest, your comment is unpublished.
    Andrew · 3 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 · 3 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 · 3 years ago
    use only standard colors ;)
  • To post as a guest, your comment is unpublished.
    Terry · 3 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.
      James Godesky · 1 years ago
      Does not work with Conditional Formatting. Set xRqPre = Nothing gives error: variable not defined (deleted the line).Screen jiggles here too, using 2010. Other wise its a cool tool..
  • To post as a guest, your comment is unpublished.
    Vic · 3 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.
      jdjshark@gmail.com · 2 years 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 · 3 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 · 3 years ago
    Fabulous fix!!