A VBA macro that automatically inserts images in bulk into Excel: reads filenames from a column, searches for images in a folder you select, then places each image neatly inside its cell — maintaining aspect ratio, centered, no distortion.
Use cases: furniture quotations, product catalogs, inventory sheets, construction project files...
- Auto-inserts images based on filenames in column C (configurable)
- Supports 15 formats: PNG, JPG, JPEG, JFIF, GIF, BMP, TIFF, WMF, EMF, WEBP, SVG, ICO, HEIC, HEIF
- Smart image sizing: preserves original aspect ratio, centered in cell
- Folder picker dialog — no hardcoded paths in code
- Removes old images before inserting (prevents overlapping)
- Reports missing files after completion
- Safety limit: max 500 images per run
- Bulk delete macro with confirmation dialog
- Path traversal protection: filenames containing
../,/, or\are rejected - Images are embedded into the workbook (not linked externally) → safe to share
- No data written outside the selected folder
- No use of
Shell,WScript, or any system commands
| Requirement | Details |
|---|---|
| OS | Windows 10/11 |
| Software | Microsoft Excel 2016 or later |
| Macros | Must be enabled in Excel |
A .bas file is a VBA (Visual Basic for Applications) source file — it cannot be run directly, it must be imported into Excel first.
Option 1 — Import file (fastest):
- Open your Excel file
- Press
Alt + F11→ open the Visual Basic Editor - Go to File → Import File...
- Select the downloaded
.basfile → click Open - Save your Excel file as
.xlsm(Excel Macro-Enabled Workbook)
Option 2 — Copy manually:
- Open the
.basfile with Notepad (right-click → Open with → Notepad) - Press
Ctrl + A→Ctrl + Cto copy everything - Open Excel → press
Alt + F11 - Go to Insert → Module
- Paste the code (
Ctrl + V) - Save your Excel file as
.xlsm
Why
.basinstead of.xlsm? A.basfile contains only plain source code — smaller, transparent, easy to review on GitHub, and won't trigger antivirus warnings like.xlsmfiles sometimes do.
Create column C with numeric image filenames (numbers only, no extension):
| A | B | C |
|---|---|---|
| # | Product Name | Image |
| 1 | Office Desk | 1 |
| 2 | Swivel Chair | 2 |
| 3 | Bookshelf | 3 |
Your image files must be named accordingly: 1.png, 2.jpg, 3.heic, etc.
Note: Column C must contain plain numbers only. Text values like
desk_001will be skipped.
- Open your Excel file
- Press
Alt + F11→ open the Visual Basic Editor - Go to Insert → Module
- Paste the entire content of the
.basfile - Press
Ctrl + S→ save as xlsm (Excel Macro-Enabled Workbook)
Two modules imported: Vietnamese (Module1) and English (Module2)
- Press
Alt + F8 - Select
InsertImagesBulk→ click Run - A folder picker dialog will appear → select the folder containing your images
- Wait for completion → review the summary message
Supported file types include
.jfif— common when saving images from Google:
- Press
Alt + F11→ open the Visual Basic Editor - In the left panel, find your file under VBAProject → double-click the module (e.g.
Module1) - Find this line near the top of the code:
Private Const COL_IMAGE As Integer = 3 ' Column C- Change
3to the desired column number — for example4for column D,5for column E - Press
Ctrl + Sto save
| Column | Number |
|---|---|
| A | 1 |
| B | 2 |
| C | 3 |
| D | 4 |
| E | 5 |
Private Const COL_IMAGE As Integer = 3 ' Column with image filenames
Private Const START_ROW As Long = 2 ' First data row (skips header)
Private Const ROW_HEIGHT_PX As Double = 80 ' Row height in points
Private Const IMG_PADDING As Double = 8 ' Padding around image in cell
Private Const MAX_SHAPES As Long = 500 ' Maximum images per runexcel-vba-image-bulk-insert/
├── ChenAnhHangLoat_VI.bas ← Vietnamese version
├── InsertImagesBulk_EN.bas ← English version
├── README.md ← This file
└── LICENSE ← MIT License
| Issue | Cause | Fix |
|---|---|---|
| Macro won't run | Macros disabled | Click Enable Content on the yellow bar when opening the file. If no bar appears: File → Options → Trust Center → Macro Settings → Enable all macros |
| Image not found | Filename mismatch | Ensure cell value matches actual filename (without extension) |
| HEIC/HEIF error | Missing codec | Install Microsoft Store or K-Lite Codec Pack |
| SVG not showing | Old Excel version | SVG requires Excel 2016+, WEBP requires Excel 2019+ or Microsoft 365 |
MIT License — free to use, modify, and share. See LICENSE for details.
Macro VBA giúp bạn chèn hàng loạt ảnh vào Excel một cách tự động: đọc tên file từ một cột, tìm ảnh trong thư mục bạn chọn, rồi căn chỉnh ảnh gọn gàng vào từng ô — đúng tỉ lệ, căn giữa, không bị méo.
Phù hợp cho: báo giá nội thất, danh mục sản phẩm, bảng kiểm kê hàng hóa, hồ sơ dự án xây dựng...
- Chèn ảnh tự động theo tên file trong cột C (có thể thay đổi)
- Hỗ trợ 15 định dạng: PNG, JPG, JPEG, JFIF, GIF, BMP, TIFF, WMF, EMF, WEBP, SVG, ICO, HEIC, HEIF
- Tự động căn chỉnh ảnh: giữ tỉ lệ gốc, căn giữa trong ô
- Hộp thoại chọn thư mục — không cần sửa đường dẫn trong code
- Xóa ảnh cũ trước khi chèn (tránh chồng ảnh)
- Báo cáo các file không tìm thấy sau khi chạy xong
- Giới hạn an toàn: tối đa 500 ảnh/lần chạy
- Macro xóa ảnh hàng loạt kèm xác nhận
- Chống path traversal: tên file chứa
../,/,\sẽ bị từ chối - Ảnh được nhúng trực tiếp vào file (không link ngoài) → an toàn khi gửi khách
- Không ghi dữ liệu ra ngoài thư mục được chọn
- Không dùng
Shell,WScript, hay bất kỳ lệnh hệ thống nào
| Yêu cầu | Chi tiết |
|---|---|
| Hệ điều hành | Windows 10/11 |
| Phần mềm | Microsoft Excel 2016 trở lên |
| Macro | Phải bật Macro trong Excel |
File .bas là file mã nguồn VBA (Visual Basic for Applications) — không chạy trực tiếp được, phải import vào Excel trước.
Cách 1 — Import file (nhanh nhất):
- Mở file Excel của bạn
- Nhấn
Alt + F11→ mở Visual Basic Editor - Vào menu File → Import File...
- Chọn file
.basvừa tải về → nhấn Open - Lưu file Excel dưới dạng
.xlsm(Excel Macro-Enabled Workbook)
Cách 2 — Copy thủ công:
- Mở file
.basbằng Notepad (click phải → Open with → Notepad) - Nhấn
Ctrl + A→Ctrl + Cđể copy toàn bộ - Mở Excel → nhấn
Alt + F11 - Vào menu Insert → Module
- Dán code vào (
Ctrl + V) - Lưu file Excel dưới dạng
.xlsm
Tại sao dùng
.basthay vì.xlsm? File.baschỉ chứa code thuần — nhẹ hơn, minh bạch hơn, dễ xem trên GitHub, và không bị phần mềm diệt virus cảnh báo như file.xlsm.
Tạo cột C chứa số thứ tự tương ứng với tên file ảnh (chỉ ghi số, không cần đuôi file):
| A | B | C |
|---|---|---|
| STT | Tên sản phẩm | Ảnh |
| 1 | Bàn làm việc | 1 |
| 2 | Ghế xoay | 2 |
| 3 | Kệ sách | 3 |
File ảnh trong thư mục phải đặt tên tương ứng: 1.png, 2.jpg, 3.heic...
Lưu ý: Cột C chỉ nhận giá trị số. Nếu ghi tên chữ như
ban_lv_001sẽ bị bỏ qua.
- Mở file Excel
- Nhấn
Alt + F11→ mở Visual Basic Editor - Vào menu Insert → Module
- Dán toàn bộ nội dung file
.basvào - Nhấn
Ctrl + S→ lưu dưới dạng xlsm (Excel Macro-Enabled Workbook)
Hai module đã import: tiếng Việt (Module1) và tiếng Anh (Module2)
- Nhấn
Alt + F8 - Chọn
ChenAnhHangLoat→ nhấn Run - Hộp thoại xuất hiện → chọn thư mục chứa ảnh
- Chờ macro chạy xong → xem thông báo kết quả
Hỗ trợ file
.jfif— định dạng phổ biến khi lưu ảnh từ Google:
- Nhấn
Alt + F11→ mở Visual Basic Editor - Ở panel bên trái, tìm file của bạn trong VBAProject → double-click vào module (ví dụ
Module1) - Tìm dòng này ở đầu code:
Private Const COL_IMAGE As Integer = 3 ' Cột C- Đổi
3thành số cột mong muốn — ví dụ4cho cột D,5cho cột E - Nhấn
Ctrl + Sđể lưu
| Cột | Số |
|---|---|
| A | 1 |
| B | 2 |
| C | 3 |
| D | 4 |
| E | 5 |
Private Const COL_IMAGE As Integer = 3 ' Cột chứa tên file ảnh
Private Const START_ROW As Long = 2 ' Hàng bắt đầu (bỏ qua header)
Private Const ROW_HEIGHT_PX As Double = 80 ' Chiều cao hàng (points)
Private Const IMG_PADDING As Double = 8 ' Khoảng trắng xung quanh ảnh
Private Const MAX_SHAPES As Long = 500 ' Giới hạn số ảnh tối đaexcel-vba-image-bulk-insert/
├── ChenAnhHangLoat_VI.bas ← Phiên bản tiếng Việt
├── InsertImagesBulk_EN.bas ← Phiên bản tiếng Anh
├── README.md ← Hướng dẫn này
└── LICENSE ← Giấy phép MIT
| Vấn đề | Nguyên nhân | Giải pháp |
|---|---|---|
| Macro không chạy | Macro bị tắt | Nhấn Enable Content trên thanh vàng khi mở file. Nếu không thấy thanh vàng: File → Options → Trust Center → Macro Settings → Enable all macros |
| Không tìm thấy ảnh | Sai tên file hoặc sai thư mục | Kiểm tra tên file trong ô Excel khớp với tên file thực tế |
| Lỗi HEIC/HEIF | Thiếu codec | Cài Microsoft Store hoặc K-Lite Codec Pack |
| SVG không hiện | Excel cũ | SVG cần Excel 2016+, WEBP cần Excel 2019+ hoặc Microsoft 365 |
MIT License — miễn phí sử dụng, chỉnh sửa và chia sẻ. Xem LICENSE để biết thêm chi tiết.


