เราเตรียมชุดข้อมูลตัวอย่างที่มี First Names, Last Names, และ Ages ของ 6 คน

เราต้องการคัดลอกเซลล์เดียวจากชุดข้อมูลด้วยโค้ดนี้
Sub Copy_Range()
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveCell.Offset(0, 4).Range("A1").Select
ActiveSheet.Paste
End Sub

เมื่อกด F5 บนคีย์บอร์ดเพื่อรัน ข้อความผิดพลาดจะขึ้นว่าโค้ดไม่ได้เปิดใช้งานสำหรับไฟล์ Excel นี้ มาดูวิธีแก้

วิธีแก้ที่ 1 – เปิดใช้งาน Macros สำหรับสมุดงานเดียวใน Excel
กรณี 1.1 เข้าผ่าน Security Warning
- เมื่อเปิดสมุดงาน จะเห็น Security Warning บน Formula Bar
- คลิก Enable Content เพื่อเปิดใช้งานแมโคร

- จะได้ Microsoft Excel Security Notice หากหน้าต่าง Visual Basic Editor เปิดอยู่ขณะเปิดไฟล์
- คลิก Enable Macros เพื่อเข้าถึง

กรณี 1.2 – เปิด Macros ด้วย Backstage View
- ไปแท็บ File

- คลิก Info

- เลือก Enable Content ภายใต้ Security Warning

กรณี 1.3 – เปลี่ยนการตั้งค่าใน Trust Center
- ไปแท็บ File เลือก Options

- เลือก Trust Center ในกล่อง Excel Options
- คลิก Trust Center Settings

- ไป Macro Settings ใน Trust Center dialog box
- ติ๊ก Disable VBA macros with notification

- ไป ActiveX Settings เลือกตัวเลือกตามภาพด้านล่าง

- กด OK สองครั้ง ปิดกล่องโต้ตอบทั้งหมด
กรณี 1.4 – สร้าง Trusted Documents
- ไปแท็บ File เลือก Options
- เลือก Trust Center Settings จากแท็บ Trust Center ในหน้าต่าง Excel Options

- ติ๊ก Allow documents on a network to be trusted ในส่วน Trusted Documents

- กด OK สองครั้ง
- หากต้องการยกเลิกฟีเจอร์นี้ คลิก Clear ในแท็บ Trusted Documents ปิดหน้าต่าง

วิธีแก้ที่ 2 – อนุญาต Macros ตลอดเวลาใน Excel สำหรับสมุดงานทั้งหมด
กรณี 2.1 – ใช้การเปลี่ยนแปลงใน Trust Center
- เลือก File เลือก Options

- เลือก Trust Center ไป Trust Center Settings

- ไปแท็บ Macros Settings
- ติ๊ก Enable VBA macros

- กด OK สองครั้ง ปิดหน้าต่างทั้งหมด
อ่านเพิ่มเติม: [Solved]: Macro Settings Greyed out in Excel
กรณี 2.2 – สร้าง Trusted Location
- เลือก File เลือก Options
- ไป Trust Center Settings จาก Excel Options
- ไปส่วน Trusted Location ในหน้าต่าง Trust Center
- คลิก Add new location

- ใส่โฟลเดอร์ในส่วน Path

- พิมพ์คำอธิบายในกล่อง Description เพื่อระบุไฟล์ที่เชื่อถือได้ง่าย

- ติ๊ก Subfolders of this location are also trusted หากมีโฟลเดอร์ย่อย

- กด OK สองครั้ง ปิดหน้าต่างทั้งหมด
- ทำซ้ำเพื่อเพิ่มไฟล์เท่าที่ต้องการ ไฟล์ Excel ในโฟลเดอร์เหล่านี้จะมี Macros เปิดใช้งาน
วิธีแก้ที่ 3 – เปิดใช้งาน Macros สำหรับเซสชันเดียว
- ไปแท็บ File
- ไปส่วน Info
- เลือก Advanced Options ใน Security Warning

- เลือกตัวเลือก Enable content for this session

- กด OK คุณจะสามารถรันแมโครได้เฉพาะระยะเวลาที่ Excel เปิดอยู่
วิธีปิดใช้งาน Macros ใน Excel
- เลือก File ไป Options
- เลือก Trust Center ไป Trust Center Settings
- ไป Macro Settings
- เลือกตัวเลือก Disable VBA macros without notification

- กด OK
สิ่งที่ควรจำ
- ต้องบันทึกไฟล์ Excel เป็น Macro-Enabled Workbook เพื่อใช้โค้ด VBA
- หลีกเลี่ยงการยินยอม Enable all macros ใน Trust Center เว้นแต่คุณแน่ใจว่ากำลังดาวน์โหลดอะไร อาจเปิดใช้งานมัลแวร์ได้
ดาวน์โหลดสมุดงานฝึกปฏิบัติ
บทความเกี่ยวข้อง
- [Fixed] Excel Macros Enabled But Not Working
- [Fixed!]: Unable to Enable Macros in Excel
- [Solve:] Cannot Edit a Macro on a Hidden Workbook
- Excel Macro Security
รับแบบฝึกหัด Excel ขั้นสูงพร้อมเฉลยฟรี!