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

วิธีป้องกันส่วนหัวและส่วนท้ายใน Excel

ใน Excel เราสามารถปกป้องเซลล์ได้อย่างรวดเร็วและง่ายดาย แต่คุณเคยพยายามป้องกันส่วนหัวและส่วนท้ายเพื่อป้องกันไม่ให้ผู้อื่นแก้ไขหรือไม่ โดยปกติไม่มีวิธีโดยตรงในการแก้ปัญหานี้ในแผ่นงาน แต่รหัส VBA ต่อไปนี้อาจช่วยคุณได้

ปกป้องส่วนหัวและส่วนท้ายด้วยรหัส VBA


ลูกศรสีฟ้าฟองขวา ปกป้องส่วนหัวและส่วนท้ายด้วยรหัส VBA

ส่วนหัวและส่วนท้ายมีประโยชน์สำหรับเราเมื่อพิมพ์แผ่นงานเพื่อป้องกันไม่ให้ผู้อื่นเปลี่ยนแปลงโปรดทำตามขั้นตอนต่อไปนี้:

1. กด ALT + F11 คีย์เพื่อเปิด Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

2. จากนั้นดับเบิลคลิก สมุดงานนี้ ภายใต้ โครงการ VBA เพื่อเปิดโมดูลเปล่าจากนั้นคัดลอกและวางรหัสต่อไปนี้ลงในโมดูล:

รหัส VBA: ป้องกันส่วนหัวและส่วนท้าย

Option Explicit
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Call ProtectHF
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Call ProtectHF
End Sub
Private Sub Workbook_Open()
    Call ProtectHF
End Sub
Private Sub ProtectHF()
'Updateby Extendoffice
    Application.ScreenUpdating = False
    ThisWorkbook.ActiveSheet.PageSetup.RightHeader = ""
    ThisWorkbook.ActiveSheet.PageSetup.RightFooter = ""
    ThisWorkbook.ActiveSheet.PageSetup.CenterFooter = "KTE-Sale-Report"
    ThisWorkbook.ActiveSheet.PageSetup.CenterHeader = "Created by Skyyang"
    ThisWorkbook.ActiveSheet.PageSetup.LeftHeader = ""
    ThisWorkbook.ActiveSheet.PageSetup.LeftFooter = ""
    Application.ScreenUpdating = True
End Sub

doc ป้องกันส่วนหัวส่วนท้าย 1

หมายเหตุ: คุณสามารถแทรกเนื้อหาส่วนหัวหรือส่วนท้ายลงในตำแหน่งที่เกี่ยวข้องในโค้ดด้านบน

3. จากนั้นบันทึกและปิดรหัสนี้ในขณะที่คลิก ลด เพื่อบันทึกเวิร์กบุ๊กนี้เนื้อหาส่วนหัวหรือส่วนท้ายที่แก้ไขจะถูกส่งกลับไปยังต้นฉบับโดยอัตโนมัติหากคุณพยายามเปลี่ยนส่วนหัวหรือส่วนท้ายดูภาพหน้าจอ:

doc ป้องกันส่วนหัวส่วนท้าย 2

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

🤖 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 (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
J'aime beaucoup, mais ce n'Est qu'une semi-sécurité pour l'en-tête. Je peux modifier les en-tête et les pied de page et imprimer avec la modification. Comment je pourrais empêcher toute modification exactement comme avec la protection de la feuille ???
This comment was minimized by the moderator on the site
Hello, Michael,
In fact, when you print the worksheet, the header or footer are not changed. It just changes the display in the Print Preview.
Please try it again, thank you!
This comment was minimized by the moderator on the site
Posso inserir uma imagem no cabeçalho?

Porque o meu cabeçalho é o logotipo da empresa.

Obrigada!
This comment was minimized by the moderator on the site
After shutting down and reopening the excel file, the header/footer can still be deleted. So how to fix this? 
Thank you very much!
This comment was minimized by the moderator on the site
Hello, Tracy,When you close the workbook, you should save it as Excel Macro-Enabled Workbook format, this will save the code.Reopening this file next time, please click Enable Content button from the top of the formula bar to activate the code.Please try, hope it can help you!
This comment was minimized by the moderator on the site
notas: Puede insertar el contenido del encabezado o pie de página en la ubicación correspondiente en el código anterior.
No entiendo ese paso , por favor si pueden explicarme, lo agradecería.
This comment was minimized by the moderator on the site
Hi used this with auto sheet name, file location, and page number and when saving it requires you to click in each field to print correctly.
This comment was minimized by the moderator on the site
Hi, Love the code to protect the header and footer, is there a way to change the font size, style, and color in the code so it is always replaced with a larger font?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations