Skip to main content

如何在列印 Excel 工作表時為頁首或頁尾添加背景色?

Author: Sun Last Modified: 2025-05-12

我們知道,可以將內容或圖片添加到工作表的頁首或頁尾中,這些內容在列印時會顯示。但你有沒有嘗試過為頁首或頁尾添加背景色呢?實際上,Excel 並沒有內建功能可以直接處理這個問題,但這裡我介紹一些間接的方法來實現該操作。

使用填充顏色為頁首或頁尾添加背景色

使用 VBA 為頁首和頁尾添加背景色


使用填充顏色為頁首或頁尾添加背景色

1. 點擊 頁面佈局 > 頁面設置
A screenshot showing Page Layout Page Setup option in Excel

2. 在頁面設置對話框中,於頁邊距標籤下,在頂端頁首底端頁尾文字框中輸入 0
A screenshot showing Page Setup dialog to adjust margins in Excel

3. 點擊 確定。然後選擇第一行,點擊 首頁 > 合併後居中
A screenshot of Merge and Center option

3. 接著從 填充顏色列表中選擇一種顏色,為合併的單元格添加背景色。
A screenshot of selecting fill color for Excel header

4. 重複步驟 2 和 3,為所需的頁尾行添加背景色。

現在,頁面看起來像是為頁首和頁尾添加了背景色。
A screenshot showing Excel worksheet with header and footer color


使用 VBA 為頁首和頁尾添加背景色

以下是一個 VBA 程式碼,可以快速同時為頁首和頁尾添加背景色。

1. 按 Alt + F11 鍵打開 Microsoft Visual Basic for Applications 視窗。

2. 點擊 插入 > 模組,複製並將以下程式碼粘貼到新模組中。

VBA:為頁首和頁尾添加背景色

Sub FakeHeaderFooter()
'UpdatebyExtendoffice20180628
    Dim I, J As Long
   Dim xRg As Range
    Dim xRow, xCol As Long
    Dim xRows, xCols As Long
    Dim xDivRow, xDivCol As Long
    Dim xTopArr, xButtArr As Variant
    Dim PageSize1, PageSize2 As Integer
    On Error Resume Next
    xTopArr = Array("Top Left", "", "", "Top Center", "", "", "", "")
    xButtArr = Array("Bottom Left", "", "", "Bottom Center", "", "", "", "")
    PageSize1 = 46
    PageSize2 = 8
    With ActiveSheet.PageSetup
        .TopMargin = Application.InchesToPoints(0)
        .BottomMargin = Application.InchesToPoints(0)
        .HeaderMargin = Application.InchesToPoints(0)
        .FooterMargin = Application.InchesToPoints(0)
        .BlackAndWhite = False
    End With
    Set xRg = ActiveSheet.UsedRange
    If xRg Is Nothing Then Exit Sub
    xRows = xRg(xRg.Count).Row
    xCols = xRg(xRg.Count).Column
    If xRows < 46 Then xRows = 46
    If xCol < 8 Then xCol = 8
    xDivRow = Int(xRows / PageSize1)
    xDivCol = Int(xCols / PageSize2)
    If ((xRows Mod PageSize1) > 0) And (xDivRow <> 0) Then xDivRow = xDivRow + 1
    If ((xCols Mod PageSize2) > 0) And (xDivCol <> 0) Then xDivCol = xDivCol + 1
    If xDivRow = 0 Then xDivRow = 1
    If xDivCol = 0 Then xDivCol = 1
    Set xRg = Range("A1").Resize(xDivRow * PageSize1, xDivCol * PageSize2)
    xRow = 1
    xCol = 1
    Cells.PageBreak = xlPageBreakNone
    For I = 1 To xDivRow * PageSize1 Step PageSize1 + 1
        For J = 1 To xDivCol * PageSize2 Step PageSize2
            Cells(I, J).Resize(1, PageSize2) = xTopArr
            Cells(I, J).Resize(1, PageSize2).Interior.ColorIndex = 34
            Cells(I + PageSize1, J).Resize(1, PageSize2) = xButtArr
            Cells(I + PageSize1, J).Resize(1, PageSize2).Interior.ColorIndex = 34
            Rows(I + PageSize1 + 1).PageBreak = xlManual
            Columns(J + PageSize2).PageBreak = xlManual
        Next
    Next
End Sub

A screenshot showing VBA code pasted in a module

3. 按 F5 鍵執行代碼,然後每個工作表的第一行和第 47 行都會添加指定的背景色,作為假的頁首和頁尾。
A screenshot of VBA-applied header and footer color in Excel

注意:在代碼中,你可以更改以下腳本以滿足你的需求。

1. 根據需要更改字串中的頁首和頁尾內容:

xTopArr = Array("左上", "", "", "頂部居中", "", "", "", "")

xButtArr = Array("底部左側", "", "", "底部居中", "", "", "", "")

2. 在此字串中更改背景色編號:

Cells(I, J).Resize(1, PageSize2).Interior.ColorIndex = 34

Cells(I + PageSize1, J).Resize(1, PageSize2).Interior.ColorIndex = 34


最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!