跳至主要内容

Kutools for Office — 一套工具,五种功能。事半功倍。

如何在Excel的页眉或页脚中查找和替换?

Author Sun Last modified

在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 助手:以智能执行为基础,彻底革新数据分析 |代码生成 |自定义公式创建|数据分析与图表生成 |调用Kutools函数……
热门功能:查找、选中项的背景色或标记重复项 | 删除空行 | 合并列或单元格且不丢失数据 | 四舍五入……
高级LOOKUP多条件VLookup|多值VLookup|多表查找|模糊查找……
高级下拉列表快速创建下拉列表 |依赖下拉列表 | 多选下拉列表……
列管理器添加指定数量的列 | 移动列 | 切换隐藏列的可见状态 | 比较区域与列……
特色功能网格聚焦 |设计视图 | 增强编辑栏 | 工作簿及工作表管理器 | 资源库(自动文本) | 日期提取 | 合并数据 | 加密/解密单元格 | 按名单发送电子邮件 | 超级筛选 | 特殊筛选(筛选粗体/倾斜/删除线等)……
15大工具集12项 文本工具添加文本删除特定字符等)|50+种 图表 类型甘特图等)|40+实用 公式基于生日计算年龄等)|19项 插入工具插入二维码从路径插入图片等)|12项 转换工具小写金额转大写汇率转换等)|7项 合并与分割工具高级合并行分割单元格等)| ……
Kutools支持多种语言——可选择英语、西班牙语、德语、法语、中文等40多种语言!

通过Kutools for Excel提升您的Excel技能,体验前所未有的高效办公。 Kutools for Excel提供300多项高级功能,助您提升效率并节省时间。 点击此处获取您最需要的功能……


Office Tab为Office带来多标签界面,让您的工作更加轻松

  • 支持在Word、Excel、PowerPoint中进行多标签编辑与阅读
  • 在同一个窗口的新标签页中打开和创建多个文档,而不是分多个窗口。
  • 可提升50%的工作效率,每天为您减少数百次鼠标点击!

所有Kutools加载项,一键安装

Kutools for Office套件包含Excel、Word、Outlook和PowerPoint的插件,以及Office Tab Pro,非常适合跨Office应用团队使用。

Excel Word Outlook Tabs PowerPoint
  • 全能套装——Excel、Word、Outlook和PowerPoint插件+Office Tab Pro
  • 单一安装包、单一授权——数分钟即可完成设置(支持MSI)
  • 协同更高效——提升Office应用间的整体工作效率
  • 30天全功能试用——无需注册,无需信用卡
  • 超高性价比——比单独购买更实惠