ไปยังเนื้อหาหลัก

วิธีใส่ค่าเซลล์ในส่วนหัว / ส่วนท้ายใน 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-to-header1

4. จากนั้นคลิก OKค่าเซลล์ที่ระบุนี้ได้ถูกแทรกลงในส่วนหัวด้านซ้ายของแผ่นงานปัจจุบัน คุณสามารถดูส่วนหัวได้โดยคลิก เนื้อไม่มีมัน > พิมพ์. ดูภาพหน้าจอ:doc-insert-cell-value-to-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-to-header1

4. จากนั้นคลิก OKค่าเซลล์ที่เลือกนี้ได้ถูกเพิ่มลงในส่วนท้ายซ้ายของเวิร์กชีตทั้งหมด คุณสามารถดูส่วนท้ายได้โดยคลิก เนื้อไม่มีมัน > พิมพ์. ดูภาพหน้าจอ:

doc-insert-cell-value-to-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. ทดแทน RightHeader / Footer or CenterHeader / Footer for LeftHeader / Footer ในรหัสด้านบนหากคุณต้องการให้ส่วนหัวหรือส่วนท้ายอยู่ในตำแหน่งที่แตกต่างกัน


ถ้าคุณต้องการแทรกข้อมูล filer ลงในส่วนหัวหรือส่วนท้ายเช่นชื่อแผ่นงาน / สมุดงานเส้นทางสมุดงานเป็นต้นคุณสามารถใช้ Kutools สำหรับ Excel's แทรกข้อมูลสมุดงาน ยูทิลิตี้หากคุณติดตั้ง Kutools for Excel

Kutools สำหรับ Excel, ที่มีมากกว่า 300 ฟังก์ชั่นที่มีประโยชน์ทำให้งานของคุณง่ายขึ้น 

หลังจาก ติดตั้งฟรี Kutools สำหรับ Excel โปรดทำดังนี้:

1 คลิก Kutools พลัส > สมุดงาน > แทรกข้อมูลสมุดงาน. ดูภาพหน้าจอ:
doc kte1

2 ใน แทรกข้อมูลสมุดงาน โต้ตอบตรวจสอบข้อมูลที่คุณต้องการแทรกด้านล่าง ข้อมูล และตรวจสอบ ส่วนหัว or ฟุตบอล ตามที่คุณต้องการ.

เคล็ดลับ: คุณสามารถแทรกข้อมูลที่ตำแหน่งสามตำแหน่งของส่วนหัวหรือส่วนท้าย: กึ่งกลางซ้ายหรือขวา
doc แทรกเส้นทางสมุดงาน 1

3 คลิก Ok. จากนั้นคุณสามารถไปที่ รายละเอียด > เค้าโครงหน้ากระดาษ เพื่อดูส่วนหัว
doc kte3

ด้วยแทรกข้อมูลสมุดงานคุณยังสามารถแทรกข้อมูลไฟล์ลงในเซลล์หรือช่วงของเซลล์ได้ คลิกที่นี่เพื่อทราบข้อมูลเพิ่มเติมเกี่ยวกับยูทิลิตี้นี้



เคล็ดลับหากคุณต้องการแยกสมุดงานออกเป็นไฟล์สมุดงาน / pdf หรือไฟล์ csv หลายไฟล์แยกกันอย่างรวดเร็วโปรดลองใช้ Kutools for Excel's แยกสมุดงาน ดังที่แสดงในภาพหน้าจอต่อไปนี้ ฟังก์ชั่นเต็มรูปแบบโดยไม่มีข้อ จำกัด ใน 30 วัน โปรดดาวน์โหลดและทดลองใช้ฟรีทันที

สมุดงานแยก doc

บทความที่เกี่ยวข้อง:

วิธีแทรกชื่อไฟล์หรือเส้นทางลงในเซลล์ / ส่วนหัวหรือส่วนท้ายใน Excel

วิธีแทรกและลบส่วนหัวส่วนท้ายและรูปภาพส่วนหัวใน Excel

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

🤖 Kutools AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 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