Skip to main content

如何在 Excel 的頁首或頁尾中尋找和替換?

Author: Sun Last Modified: 2025-08-06

在 Excel 中,尋找特定值並將其替換為另一個值是一項常見的任務,但標準的尋找和替換功能僅適用於選定區域、工作表和工作簿內,無法用於頁首和頁尾。在本文中,我將介紹一種宏代碼,用於在頁首和頁尾中尋找和替換值。

使用 VBA 在頁首或頁尾中尋找和替換


arrow blue right bubble 使用 VBA 在頁首或頁尾中尋找和替換

1. 激活您要在頁首和頁尾中尋找和替換文字的工作表,按下 Alt + F11 鍵以打開 Microsoft Visual Basic for Applications 視窗。

2. 點擊 插入 > 模組,然後將以下 VBA 代碼粘貼到模組中。參見截圖:

VBA:在頁首和頁尾中尋找和替換

Sub FnR_HF()
'UpdateByExtendoffice20160623
    Dim xStr As String, xRep As String
    'Const csTITLE As String = "Find and Replace"
    On Error Resume Next
    xStr = Application.InputBox("Replace what", "Kutools for Excel", , , , , 2)
    If xStr = "" Then Exit Sub
    xRep = Application.InputBox("With what", , "Kutools for Excel", , , , , 2)
    With ActiveSheet.PageSetup
        ' Substitute Header/Footer values
        .LeftHeader = Application.WorksheetFunction.Substitute(.LeftHeader, xStr, xRep)
        .CenterHeader = Application.WorksheetFunction.Substitute(.CenterHeader, xStr, xRep)
        .RightHeader = Application.WorksheetFunction.Substitute(.RightHeader, xStr, xRep)
        .LeftFooter = Application.WorksheetFunction.Substitute(.LeftFooter, xStr, xRep)
        .CenterFooter = Application.WorksheetFunction.Substitute(.CenterFooter, xStr, xRep)
        .RightFooter = Application.WorksheetFunction.Substitute(.RightFooter, xStr, xRep)
    End With
End Sub

A screenshot of the VBA code editor

3. 然後按 F5 鍵運行代碼,隨後會彈出一個對話框讓您輸入要查找的文字。參見截圖:
A screenshot of the dialog box for entering text to find in the Excel header/footer

4. 點擊 確定,接著會彈出另一個對話框讓您輸入要替換為的文字。參見截圖:
A screenshot of the dialog box for entering text to replace in the Excel header/footer

5. 點擊 確定。現在頁首和頁尾中的 “KTE” 已被替換為 “Kutools for Excel”
A screenshot showing the replaced text in the Excel header/footer

提示:如果您想將此操作應用於整個工作簿,可以使用以下宏代碼。

Sub FnR_HF()
'UpdateByExtendoffice20171122
    Dim I As Long
    Dim xStr As String, xRep As String
    'Const csTITLE As String = "Find and Replace"
    On Error Resume Next
    xStr = Application.InputBox("Replace what", "Kutools for Excel", , , , , 2)
    If xStr = "" Then Exit Sub
    xRep = Application.InputBox("With what", , "Kutools for Excel", , , , , 2)
    For I = 1 To ActiveWorkbook.Sheets.Count
        With Sheets(I).PageSetup
            ' Substitute Header/Footer values
            .LeftHeader = Application.WorksheetFunction.Substitute(.LeftHeader, xStr, xRep)
            .CenterHeader = Application.WorksheetFunction.Substitute(.CenterHeader, xStr, xRep)
            .RightHeader = Application.WorksheetFunction.Substitute(.RightHeader, xStr, xRep)
            .LeftFooter = Application.WorksheetFunction.Substitute(.LeftFooter, xStr, xRep)
            .CenterFooter = Application.WorksheetFunction.Substitute(.CenterFooter, xStr, xRep)
            .RightFooter = Application.WorksheetFunction.Substitute(.RightFooter, xStr, xRep)
        End With
    Next
End Sub

最佳 Office 辦公效率工具

🤖 Kutools AI Aide:徹底革新數據分析,基於智能執行|生成程式碼|創建自訂公式|分析數據並生成圖表|調用 Kutools Functions
熱門功能查找、選取項目的背景色或標記重複值 | 刪除空行 | 合併列或單元格且不丟失資料 | 四捨五入...
高級 LOOKUP多條件查找|多值查找|多表查找|模糊查找...
高級下拉列表快速創建下拉列表 | 關聯下拉列表 | 多選下拉列表 ...
列管理器添加指定數量的列 | 移動列 | 切換隱藏列的可見狀態 | 區域與列比較 ...
精選功能網格聚焦 | 設計檢視 | 增強編輯欄 | 工作簿及工作表管理器 | 資源庫 (自動文本) | 日期提取器 | 合併資料 | 加密/解密儲存格 | 按清單發送電子郵件 | 超級篩選 | 特殊篩選(篩選粗體/傾斜/刪除線 ...)...
前15 大工具集12 款文本工具添加文本刪除特定字符,...)|50+ 種圖表 類型甘特圖,...)|40+ 實用 公式基於生日計算年齡,...)|19 款插入工具插入QR码按路徑插入圖片,...)|12 款轉換工具金額轉大寫匯率轉換,...)|7 款合併和分割工具高級合併行分割儲存格,...)| ...以及更多
使用 Kutools 支援你的語言——支援英語、西班牙語、德語、法語、中文及40 多種語言!

利用 Kutools for Excel 大幅提升你的 Excel 技能,感受前所未有的高效體驗。 Kutools for Excel 提供超過300 項高級功能,助你提升效率並保存時間。 點此查看你最需要的功能...


Office Tab 為 Office 帶來標籤式介面,讓你的工作更加輕鬆

  • 啟用 Word、Excel、PowerPoint 的標籤式編輯和閱讀功能
  • 在同一個視窗的標籤中打開和創建多個文件,而不是在新窗口中分開開啟。
  • 可提升你50% 的工作效率,每天為你大量減少滑鼠點擊次數!