跳到主要內容

如何將單元格值放在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.您也可以將上述代碼用於將單元格值插入右側的頁眉/頁腳或中心頁眉/頁腳,只需替換 左標題 / 左腳 with 正確的標題 / 右腳 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 中心頁眉/頁腳 對於 左頁眉/頁腳 如果您希望頁眉或頁腳的位置不同,請在上述代碼中。


如果要在頁眉或頁腳中插入文件管理器信息,例如工作表/工作簿名稱,工作簿路徑等,則可以使用 Excel的Kutools插入工作簿信息 實用程序,如果您已經安裝了Kutools for Excel。

Excel的Kutools, 與以上 300 方便的功能,使您的工作更加輕鬆。 

免費安裝 Kutools for Excel,請執行以下操作:

1。 點擊 Kutools 加 > 工作簿 > 插入工作簿信息。 看截圖:
文檔1

2。 在裡面 插入工作簿信息 對話框中,檢查您需要在下面插入的信息 資訊 部分,並檢查 or 頁腳 隨你便。

小提示: 您可以在頁眉或頁腳的三個位置插入信息:中心,左側或右側。
doc插入工作簿路徑1

3。 點擊 Ok。 那你可以去 瀏覽 > 頁面佈局 查看標題。
文檔3

使用“插入工作簿信息”,您還可以將文件信息插入到一個或多個單元格中。 單擊此處了解有關此實用程序的更多信息。



小費。如果您想將一個工作簿快速拆分為多個工作簿/ pdf文件或csv文件,請嘗試使用Kutools for Excel的 拆分工作簿 如以下屏幕截圖所示。 它在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選項卡為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