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

วิธีบังคับให้ผู้ใช้บันทึกเป็นสมุดงานที่เปิดใช้งานมาโคร

เมื่อคุณบันทึกสมุดงาน Excel สมุดงานนั้นจะถูกบันทึกเป็นรูปแบบไฟล์ xlsx ตามค่าเริ่มต้นและรูปแบบไฟล์นี้จะกำจัดรหัสมาโครออกจากสมุดงานหากมีหลายรหัส ในการเก็บรหัสคุณควรบันทึกเวิร์กบุ๊กเป็นรูปแบบ Excel Macro-Enable Workbook คุณจะบังคับให้ผู้ใช้บันทึกเป็นสมุดงานที่เปิดใช้งานมาโครได้อย่างไร

บังคับให้ผู้ใช้บันทึกเป็นสมุดงานที่เปิดใช้งานมาโครด้วยรหัส VBA


ลูกศรสีฟ้าฟองขวา บังคับให้ผู้ใช้บันทึกเป็นสมุดงานที่เปิดใช้งานมาโครด้วยรหัส VBA

หากต้องการบันทึกสมุดงานเป็นสมุดงานที่เปิดใช้งานแมโครโดยค่าเริ่มต้นรหัส VBA ต่อไปนี้อาจช่วยคุณได้โปรดดำเนินการดังนี้:

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

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

รหัส VBA: บังคับให้บันทึกสมุดงานเป็นสมุดงานที่เปิดใช้งานแมโครโดยค่าเริ่มต้น:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Updateby Extendoffice
Dim xFileName As String
If SaveAsUI <> False Then
    Cancel = True
    xFileName = Application.GetSaveAsFilename(, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm", , "Save As xlsm file")
    If xFileName <> "False" Then
      Application.EnableEvents = False
      ActiveWorkbook.SaveAs Filename:=xFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
      Application.EnableEvents = True
    Else
      MsgBox "Action Cancelled"
      Cancel = True
      Exit Sub
    End If
End If
End Sub

doc force บันทึกเป็น xlsm 1

3. จากนั้นบันทึกและปิดหน้าต่างรหัสนี้นับจากนี้ไปเมื่อผู้ใช้บันทึกเป็นสมุดงานนี้ระบบจะบันทึกเป็น สมุดงานที่เปิดใช้งานแมโคร Excel โดยค่าเริ่มต้นตามภาพหน้าจอต่อไปนี้:

doc force บันทึกเป็น xlsm 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 (11)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
The VBA code forcing user to save in .xlsm file format works very well for me. I was looking for this solution for quite some time. Many thanks for the help!
This comment was minimized by the moderator on the site
Any way to have this VBA in a file that is a true template file type?
This comment was minimized by the moderator on the site
When you are ready to create the template, I believe you must use the Immediate Window to run
ActiveWorkbook.SaveAs Filename:="ENTER YOUR TEMPLATE NAME", FileFormat:=xlOpenXMLTemplateMacroEnabled
This comment was minimized by the moderator on the site
Any way to have this VBA in a template file
This comment was minimized by the moderator on the site
Keep getting a 1004 ERROR.
Using Off365 2016 for MAC. Got a solutions?
This comment was minimized by the moderator on the site
Hello, Jonathan,
This code works well in Windows Office, but it not tested in MAC.
You should search for a code for working in MAC.
Thank you!
This comment was minimized by the moderator on the site
fileformat: =52 is a nicer was as making macro enabled workbook. fileformat:=51 is normal workbook.
This comment was minimized by the moderator on the site
I'm using Excel 2016 for Mac, and when I use this VBA code to achieve the "force save as .xlsm" I get the following error:

Run-time error '1004':

Method 'GetSaveAsFilename' of object '_Application' failed

Any guidance someone could provide around this error and how I may need to adjust the code to work on the Mac version of Excel would be greatly appreciated!
This comment was minimized by the moderator on the site
Nice one!

Saved me a lot of headache! :)
This comment was minimized by the moderator on the site
Saved me a lot of pain - thanks so much
This comment was minimized by the moderator on the site
This was so useful and worked like a charm thank you :)
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations