打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何查找和替换Excel中的页眉或页脚?

查找特定值,然后用Excel中的另一个值替换在我们的日常工作中很常见,但在Excel中,查找和替换功能在选择,工作表和工作簿中运行良好,但在页眉和页脚中找不到和替换。 在这篇文章中,我介绍一个宏代码来查找和替换页眉和页脚中的值。

用VBA查找并替换页眉或页脚


导航窗格 - 查找和替换

Kutools for Excel先进的 查找和替换 功能,可以帮你找到和
替换多个工作表和工作簿中的值。


用VBA查找并替换页眉或页脚


1。 启用要查找并替换页眉和页脚的工作表,按 Alt + F11键 键打开 Microsoft Visual Basic for Applications 窗口。

2。 点击 插页 > 模块,并将VBA代码粘贴到模块下面。 看截图:

VBA:查找并替换页眉和页脚

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

3。 然后按 F5 键运行代码,然后出现一个对话框,让您输入要查找的文本。 看截图:

4。 点击 OK,另一个对话框弹出,输入要替换的文本。 看截图:

5。 点击 OK。 现在的价值 “KTE” 已被替换 “Excel的Kutools” 在页眉和页脚。

小技巧:如果要为整个工作簿工作,可以使用下面的宏代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
vba excel 去掉单元格中隐藏着的双引号或单引号 (多列含有,希望得到一次性清楚)
WORD中如何删除掉某几页的页眉和页脚
Excel2010设置奇偶页页眉页脚不同
千问系列教程之第十七问excel中如何添加页眉页脚
快速应用excel2007页眉和页脚样式
【Excel】在页眉/页脚中添加文件路径
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服