這里只講解第三種方法:調用barcode控件生成條形碼。希望能夠拋磚引玉,這里只是為大家提供一個(gè)思路。
上面說(shuō)的生成二維碼的excel自帶控件就是Microsoft BarCode控件,利用插入控件來(lái)做條形碼,每次只能生成一個(gè)。所以,我們用VBA代碼來(lái)實(shí)現批量生成條形碼。
還有一點(diǎn),這里只討論Code 39與code 128條形碼的生成。
批量生成條形碼效果如下:
實(shí)現代碼:先建立簡(jiǎn)單窗體,用于讓用戶(hù)選擇條形碼類(lèi)型。
Private Sub CommandButton1_Click() '點(diǎn)擊窗體中的確定按鈕,運行一下程序
Dim i As Integer, n As Integer, str As Byte
For i = 1 To 2
If Me.Controls("OptionButton" & i).Value = True Then '當單選框被選中的時(shí)候,確定一種條形碼類(lèi)型
If Me.Controls("OptionButton" & i).Caption = "Code-39" Then
str = 6
ElseIf Me.Controls("OptionButton" & i).Caption = "Code-128" Then
str = 7
End If
End If
Next
Application.ScreenUpdating = False
i = Range("A1048576").End(xlUp).Row
For j = 1 To i
If Cells(j, 1) <> "" Then
With Sheet1.OLEObjects.Add(ClassType:="BARCODE.BarCodeCtrl.1") '利用控件屬性,定義條形碼尺寸和類(lèi)型
.Object.Style = str
.Object.Value = Cells(j, 1).Value
.Height = Cells(j, 2).Height
.Width = Cells(j, 2).Width
.Left = Cells(j, 2).Left
.Top = Cells(j, 2).Top
End With
End If
Next
Unload Me'卸載窗體
Application.ScreenUpdating = True
End Sub
聯(lián)系客服