Ví dụ https://quickchart.io/qr?text=XIN CHAO&size=150
Tìm các cú pháp để tạo ra QR Code.
Như url trên, thì https://quickchart.io/qr?text=XIN CHAO&size=150
Chữ màu đỏ chính là nội dung tạo mã vạch mà bạn mong muốn tạo.
Copy code VBA vào sự kiện nút lệnh
Sub TaoQRCode()
Dim noidungtao As String
Dim QRCodeURL As String
Dim ws As Worksheet
Dim img As Picture
Dim FilePath As String
' Lấy dữ liệu từ ô A1
noidungtao = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
QRCodeURL = "https://quickchart.io/qr?text=" & noidungtao & "&size=150"
' Tạo đường dẫn file tạm để lưu ảnh
FilePath = Environ$("temp") & "\qrcode.png"
' Tải ảnh từ URL và lưu vào file tạm
DownloadFile QRCodeURL, FilePath
' Xóa mã QR cũ nếu có
On Error Resume Next
ThisWorkbook.Sheets("Sheet1").Pictures("QRCode").Delete
On Error GoTo 0
' Thêm hình ảnh mã QR vào sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Set img = ws.Pictures.Insert(FilePath)
' Đặt tên và vị trí ảnh
With img
.Name = "QRCode"
.Top = ws.Range("B1").Top
.Left = ws.Range("B1").Left
.Width = 150
.Height = 150
End With
End Sub
Sub DownloadFile(ByVal URL As String, ByVal SavePath As String)
Dim WinHttp As Object
Set WinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
WinHttp.Open "GET", URL, False
WinHttp.Send
If WinHttp.Status = 200 Then
Dim adoStream As Object
Set adoStream = CreateObject("ADODB.Stream")
adoStream.Type = 1 ' Binary
adoStream.Open
adoStream.Write WinHttp.ResponseBody
adoStream.SaveToFile SavePath, 2 ' Overwrite if exists
adoStream.Close
Else
MsgBox "Không thể tải mã QR. HTTP Status: " & WinHttp.Status
End If
End Sub
- Dùng API tạo QR Code đáng tin cậy.
- Nên hiểu VBA Excel ở cơ bản để tạo được những ứng dụng hay về tạo mã vạch QR Code.
* Tôi có thể học VBA Excel ở đâu nhanh nhất ?
* Trả lời: bạn tham khảo khóa học VBA Excel 1 giờ
- Nên đặt vào ô cụ thể như Range("B1") để hình không bị lệch hoặc chồng.
- Có thể dùng Worksheet_Change để tự động cập nhật
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
Call TaoQRCode
End If
End Sub
- Nên xoá file ảnh tạm sau khi dùng (nếu cần bảo mật)
Tìm kiếm:
Hướng dẫn tạo mã QRCode từ VBA Excel từ A đến Z