Function QR_Code(Target As Range, MyURL As String)
Dim MyPic As Picture
Dim pic As Picture
With ActiveSheet
For Each pic In .Pictures
If pic.TopLeftCell.Address = Target.Address Then Set MyPic = pic '儲存格內有條碼就以此圖片顯示
Next
If MyPic Is Nothing Then '若儲存格內無圖就插入QR Code圖片
Set MyPic = .Pictures.Insert("https://chart.googleapis.com/chart?chs=100x100&cht=qr&chl=" & MyURL)
MyPic.Top = Target.Top + (Target.Height - MyPic.Height) / 2 '圖片垂直置中
MyPic.Left = Target.Left + (Target.Width - MyPic.Width) / 2 '圖片水平置中
End If
Set mychart = .Shapes.AddChart '設置圖表長寬
mychart.Height = MyPic.Height
mychart.Width = MyPic.Width
MyPic.Copy '複製QR Code
mychart.Chart.Paste '貼到圖表
mychart.Chart.Export "D:\code.jpg" '圖表匯出圖檔
MyPic.ShapeRange.Fill.UserPicture "D:\code.jpg" '更換QR Code
mychart.Delete '刪除圖表
Kill "D:\code.jpg" '刪除圖表圖檔
End With
End Function