2016-11-24 95 views
0

只是一個簡短的問題,如何在Excel工作表中生成條形碼?條碼文本已在單元格中指定。如何在excel工作表中生成條形碼

(不原意質量產生否則可以在MS Word中進行條形碼。)

+0

只需安裝條形碼字體? – Comintern

+0

@Comintern是的,好點,但我不想問大家安裝的東西... – Rosetta

回答

1

選取條形碼文字的書寫範圍:

enter image description here

然後運行以下腳本:

Sub INSERT_BARCODE() 
    Const BarcodeWidth As Integer = 156 
    Dim ws As Worksheet, WdApp 
    Set ws = ActiveSheet 
    Set WdApp = CreateObject("Word.Application") 
    With WdApp.Documents.Add 
     .PageSetup.RightMargin = .PageSetup.PageWidth - .PageSetup.LeftMargin - BarcodeWidth 
     .Fields.Add(Range:=.Range, Type:=-1, Text:="DISPLAYBARCODE " & CStr(Selection.Value) & " CODE39 \d \t", PreserveFormatting:=False).Copy 
    End With 
    ws.PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False, DisplayAsIcon:=False 
    WdApp.Quit SaveChanges:=False 
    Set WdApp = Nothing 
End Sub 

enter image description here

注:

BR〜

+0

我得到一個'錯誤!在運行該代碼時收到未定義的消息。 –

+0

我一定在word.application部分丟失了一些東西。我正在做Excel/Word 2013.你在2010年? – Rosetta

+0

是的,我在Word 2010. –

1

這是大規模過度specced您所需要的,但您可以根據需要拉位出來。

Sub Call_Barcode_Service() 

Dim strResource As String 
Dim strSize As String 
Dim iHgt As Integer 
Dim iWth As Integer 
Dim iGap As Integer 
Dim PictureGrab As String 
Dim lngLastRow As Long 

strSize = UCase(InputBox("How Big?", "Small, Medium or Large?", "L")) 

Select Case strSize 

    Case Is = "S" 
     iWth = 150 
     iHgt = 45 
     iGap = 3 


    Case Is = "M" 
     iWth = 150 
     iHgt = 60 
     iGap = 4 

    Case Is = "L" 
     iWth = 240 
     iHgt = 75 
     iGap = 5 

    Case Else 
     iWth = 250 
     iHgt = 75 
     iGap = 5 

End Select 


Set sel = Selection.SpecialCells(xlTextValues) 

Set news = Worksheets.Add() 
news.Name = "Barcodes" 
Set op = news.Range("A1") 

For Each acc In sel 
strResource = acc.Value 

PictureGrab = "http://www.barcodesinc.com/generator/image.php?code=" & strResource & "&style=197&type=C128B&width=" & iWth & "&height=" & iHgt & "&xres=1&font=1" 

Set sh = ActiveSheet.Shapes.AddShape(msoShapeRectangle, op.Left, op.Top, iWth, iHgt) 

With sh 

.Name = strResource 
.Line.Visible = False 
.Fill.UserPicture PictureGrab 

End With 

Set op = op.Offset(iGap + 1, 0).Range("A1") 

Next 

Range("G1").Select 

End Sub 
+1

不錯的一個。我也喜歡這個,適用於所有的辦公版本。但需要互聯網連接,除非圖片是靜態的。 – Rosetta

+0

是的,這是需要互聯網訪問的一個警告。我使用代碼來運行**數百行數據,並使用類似的「VBA」來生成QR代碼,以顯示條形碼和QR之間的數據密度差異。就像我說的那樣,過度專注於一個細胞! :O) –