跳到主要内容

如何将单元格值放在Excel中的页眉/页脚中?

我们可以使用Excel的功能轻松插入包含文件路径,文件名,当前日期或其他信息的页眉或页脚,但是有时,我们想使用单元格值作为Excel中的页眉或页脚。 我们如何将单元格的内容放入工作簿的页眉或页脚中?

使用VBA代码将单元格值放在工作表的页眉或页脚中

使用VBA代码将单元格值放在所有工作表的页眉或页脚中

使用 Kutools for Excel 将文件信息插入页眉/页脚 好主意3


如果要将单元格内容放在当前工作表的页眉或页脚中,则以下VBA代码可以为您提供帮助。 请执行以下步骤:

1。 激活您要插入带有单元格值的页眉或页脚的工作表,然后按住 ALT + F11 键,然后打开 Microsoft Visual Basic for Applications窗口.

2点击 插页 > 模块,然后将以下代码粘贴到“模块窗口”中。

VBA代码:将指定的单元格值放在工作表的标题中

Sub HeaderFrom()
'Update 20140318
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection.Range("A1")
Set WorkRng = Application.InputBox("Range (single cell)", xTitleId, WorkRng.Address, Type:=8)
Application.ActiveSheet.PageSetup.LeftHeader = WorkRng.Range("A1").Value
End Sub

3。 然后按 F5 键以运行该代码,将弹出一个对话框,提醒您选择一个要将其内容放入标题的单元格。doc-insert-cell-value-header1

4。 然后点击 OK,此指定的单元格值已插入到当前工作表的左侧标题中。 您可以通过单击查看标题 文件 > 打印。 看截图:doc-insert-cell-value-header1

笔记:

1.如果要将单元格内容用作活动工作表的页脚,则可以应用以下代码:

VBA代码:将指定的单元格值放在工作表的页脚中

Sub FooterFrom()
'Update 20140318
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection.Range("A1")
Set WorkRng = Application.InputBox("Range (single cell)", xTitleId, WorkRng.Address, Type:=8)
Application.ActiveSheet.PageSetup.LeftFooter = WorkRng.Range("A1").Value
End Sub

2.您也可以将上述代码用于将单元格值插入右侧的页眉/页脚或中心页眉/页脚,只需替换 左标题 / 左脚正确的标题 / 右脚 or 中心头 / 中心脚 在代码中。


有时,您想将带有选定单元格内容的页眉或页脚插入到打开的工作簿的所有工作表中,使用上述代码,您需要一次又一次地重复它们。 在这种情况下,下面的VBA代码可以立即将单元格内容添加到整个工作簿的页眉或页脚中。

1。 激活您要在其中插入带有单元格值的页眉或页脚的工作簿,然后按住 ALT + F11 键,然后打开 Microsoft Visual Basic for Applications窗口.

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块窗口.

VBA代码:将指定的单元格值放在所有工作表的页脚中

Sub AddFooterToAll()
'Update 20140318
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection.Range("A1")
Set WorkRng = Application.InputBox("Range (single cell)", xTitleId, WorkRng.Address, Type:=8)
For Each ws In Application.ActiveWorkbook.Worksheets
    ws.PageSetup.LeftFooter = WorkRng.Range("A1").Value
Next
End Sub

3。 然后按 F5 键以执行此代码,将弹出一个对话框,提醒您选择一个单元格,将其内容放在整个工作簿的页脚中。

doc-insert-cell-value-header1

4。 然后点击 OK,此选定的单元格值已添加到所有工作表的左脚。 您可以通过单击查看页脚 文件 > 打印。 看截图:

doc-insert-cell-value-header1

笔记:

1.如果要将单元格内容用作整个工作簿的标题,则可以应用以下代码:

VBA代码:将指定的单元格值放在所有工作表的标题中

Sub AddHeaderToAll()
'Update 20140318
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection.Range("A1")
Set WorkRng = Application.InputBox("Range (single cell)", xTitleId, WorkRng.Address, Type:=8)
For Each ws In Application.ActiveWorkbook.Worksheets
    ws.PageSetup.LeftHeader = WorkRng.Range("A1").Value
Next
End Sub

2.替代 右页眉/页脚 or 中心页眉/页脚 左页眉/页脚 如果您希望页眉或页脚的位置不同,请在上述代码中使用。


如果要在页眉或页脚中插入文件管理器信息,例如工作表/工作簿名称,工作簿路径等,则可以使用 Kutools for Excel插入工作簿信息 实用程序如果您已经安装了 Kutools for Excel。

Kutools for Excel, 与超过 300 方便的功能,使您的工作更加轻松。 

免费安装 Kutools for Excel,请执行以下操作:

1。 点击 Kutools 加 > 工作簿 > 插入工作簿信息。 看截图:
文档1

2.在 插入工作簿信息 对话框中,检查您需要在下面插入的信息 资讯 部分,并检查 标题 or 页脚 随你便。

提示: 您可以在页眉或页脚的三个位置插入信息:中心,左侧或右侧。
doc插入工作簿路径1

3。 点击 Ok。 那你可以去 查看 > 页面布局 查看标题。
文档3

使用“插入工作簿信息”,您还可以将文件信息插入到一个或多个单元格中。 单击此处了解有关此实用程序的更多信息。



小费。如果您想快速将一个工作簿分别拆分为多个工作簿/pdf文件或csv文件,请尝试使用Kutools for Excel's 拆分工作簿 如以下屏幕截图所示。 它在30天内无限制地提供完整功能, 请下载并立即免费试用。

doc拆分工作簿

相关文章:

如何在Excel中的单元格/页眉或页脚中插入文件名或路径?

如何在Excel中插入和删除页眉,页脚和页眉图片?

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (9)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
This doesn't appear to make a dynamic link, i.e., the value entered into the header doesn't change when the value of the cell does. So what is the purpose of the VBA code, when a copy/paste will do the same?
This comment was minimized by the moderator on the site
Hello, Neil,
If you want to link the cell value to the header or footer dynamically, please apply the following VBA code:

Note: You should insert the code into the sheet code not the normal Module.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim WorkRng As Range
Dim xStR As String
On Error Resume Next
xStR = "A1" '
Set WorkRng = Intersect(Application.ActiveSheet.Range("A1"), Target)
If WorkRng Is Nothing Then Exit Sub
Application.ActiveSheet.PageSetup.RightHeader = WorkRng.Range("A1").Value
End Sub


Please have a try, hope it can help you
This comment was minimized by the moderator on the site
maksudnya bagaimana?
This comment was minimized by the moderator on the site
Hi,

This appears to only work once.
What if the value of the cell changes?
Is there a way to link the cell so the header changes when the cell value changes?

I have a workbook where I have three sheets.

Sheet 1 labeled "Deletion Sheet" - Sheet we send to the warehouse with info what to palatalize for the order
Sheet 2 labeled "OA" - My order acknowledgement to the customer which pulls most of the data from the first sheet including the Order number which I need to have in my header. So I am trying to link the Header to the cell in this page with the order number (F5) which gets it's value from (C7) in the first work sheet ("Deletion Sheet")
Sheet 3 labeled "Invoice - Invoice which also pulls most of the same information from the first sheet which would also need the header to include the value (F5) of this sheet taken from (C7) in "Deletion Sheet"
This comment was minimized by the moderator on the site
When using your "VBA code: put a specified cell value in header of all worksheets", I would like the value placed to be formatted: Tahoma, bold, in font size 12.
How can this be added to your code?
This comment was minimized by the moderator on the site
Hi There, Is there a way that you can add a cell value which takes account of applied filters? For example... A1 = Monday A2 = Tuesday A3 = Wednesday. Using the VBA code to display cell A1 will work initially, but once I apply a filter on days of the week, the "top" cell value is no longer A1. Is there a way to pick up the variable? Many thanks,
This comment was minimized by the moderator on the site
I want to insert four cells in header.This VB is only for one cell. How can I do it. Thanks
This comment was minimized by the moderator on the site
You can try Concatenating the value of rht four cells into a single cell and then use the single cell as the header.
This comment was minimized by the moderator on the site
I needed to insert an active payroll date range into multiple sheets. The user opens the Payroll Date sheet, enters the date range and before she prints it updates all the sheets in the workbook. Here is how I am able to insert multiple cells into the range with a bit less code 2010 compatible: Private Sub Workbook_BeforePrint(Cancel As Boolean) Dim WorkRng As Range On Error Resume Next For Each ws In Application.ActiveWorkbook.Worksheets ws.PageSetup.RightHeader = Range("'Payroll Date'!A1").Value & vbCr & Range("'Payroll Date'!A2").Value Next End Sub
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations