假设从B1单元格取值,在A1单元格中生成二维码,
那么,代码如下↓
Sub genBarCode()
'清除已有二维码
Call clearBarCode
With ActiveSheet.OLEObjects.Add(ClassType:="BARCODE.BarCodeCtrl.1")
.Object.Style = 11 '二维码样式
.Object.Value = Range("B1").Value '二维码内容
'二维码大小和单元格A1相同
.Height = Cells(1, 1).Height
.Width = Cells(1, 1).Width
.Left = Cells(1, 1).Left
.Top = Cells(1, 1).Top
End With
End Sub
Sub clearBarCode()
Dim shp As Shape
For Each shp In ActiveSheet.Shapes
If shp.Type = 12 Then shp.Delete
Next
End Sub
生成的二维码如下↓