ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 5183|回复: 20

纯VBA生成条形码

[复制链接]

TA的精华主题

TA的得分主题

发表于 2022-9-1 17:23 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 心电感应 于 2022-9-1 18:54 编辑

VBA生成条形码的方式,常见的方法有:

1. 字体法(需要安装字体)
2. 控件法(需要引用第三方控件)
都不是很方便。既然二维码都可以纯代码生成。那条形码可以纯代码生成吗?答案是肯定的。生成的条形码可以很方便的用于商品管理、单据管理等领域。
下面是转贴,转自"Code128" barcode generator in VBA | MrExcel Message Board

亲测代码有效。代码如下:
  1. <code>
  2. Sub Code128Generate(ByVal X As Single, ByVal Y As Single, ByVal Height As Single, ByVal LineWeight As Single, _
  3.                   ByRef TargetSheet As Worksheet, ByVal Content As String)
  4. ' Supports B and C charsets only; values 00-94, 99,101, 103-105 for B, 00-101, 103-105 for C
  5. ' X, Y - top-left corner coordinates
  6. ' X in mm (0.376042)
  7. ' Y in mm (0.341)
  8. ' Height in mm
  9. ' LineWeight in pt

  10. Const Tbar_Symbol As String * 2 = "11" ' termination bar
  11. Dim WeightSum As Single
  12. Dim CurBar As Integer
  13. Dim i, j, k, FirstSymbol As Integer
  14. Dim tstr2 As String * 2
  15. Dim tstr1 As String * 1
  16. Dim ContentString As String ' bars sequence

  17. Dim SymbolValue(0 To 106) As Integer ' values
  18. Dim SymbolString(0 To 106) As String * 11 'bits sequence
  19. Dim SymbolCharB(0 To 106) As String * 1  'Chars in B set
  20. Dim SymbolCharC(0 To 106) As String * 2  'Chars in B set

  21. For i = 0 To 106 ' values
  22.     SymbolValue(i) = i
  23. Next i

  24. ' Symbols in charset B
  25. For i = 0 To 94
  26.     SymbolCharB(i) = Chr(i + 32)
  27. Next i

  28. ' Symbols in charset C
  29. SymbolCharC(0) = "00"
  30. SymbolCharC(1) = "01"
  31. SymbolCharC(2) = "02"
  32. SymbolCharC(3) = "03"
  33. SymbolCharC(4) = "04"
  34. SymbolCharC(5) = "05"
  35. SymbolCharC(6) = "06"
  36. SymbolCharC(7) = "07"
  37. SymbolCharC(8) = "08"
  38. SymbolCharC(9) = "09"
  39. For i = 10 To 99
  40.     SymbolCharC(i) = CStr(i)
  41. Next i

  42. ' bit sequences
  43. SymbolString(0) = "11011001100"
  44. SymbolString(1) = "11001101100"
  45. SymbolString(2) = "11001100110"
  46. SymbolString(3) = "10010011000"
  47. SymbolString(4) = "10010001100"
  48. SymbolString(5) = "10001001100"
  49. SymbolString(6) = "10011001000"
  50. SymbolString(7) = "10011000100"
  51. SymbolString(8) = "10001100100"
  52. SymbolString(9) = "11001001000"
  53. SymbolString(10) = "11001000100"
  54. SymbolString(11) = "11000100100"
  55. SymbolString(12) = "10110011100"
  56. SymbolString(13) = "10011011100"
  57. SymbolString(14) = "10011001110"
  58. SymbolString(15) = "10111001100"
  59. SymbolString(16) = "10011101100"
  60. SymbolString(17) = "10011100110"
  61. SymbolString(18) = "11001110010"
  62. SymbolString(19) = "11001011100"
  63. SymbolString(20) = "11001001110"
  64. SymbolString(21) = "11011100100"
  65. SymbolString(22) = "11001110100"
  66. SymbolString(23) = "11101101110"
  67. SymbolString(24) = "11101001100"
  68. SymbolString(25) = "11100101100"
  69. SymbolString(26) = "11100100110"
  70. SymbolString(27) = "11101100100"
  71. SymbolString(28) = "11100110100"
  72. SymbolString(29) = "11100110010"
  73. SymbolString(30) = "11011011000"
  74. SymbolString(31) = "11011000110"
  75. SymbolString(32) = "11000110110"
  76. SymbolString(33) = "10100011000"
  77. SymbolString(34) = "10001011000"
  78. SymbolString(35) = "10001000110"
  79. SymbolString(36) = "10110001000"
  80. SymbolString(37) = "10001101000"
  81. SymbolString(38) = "10001100010"
  82. SymbolString(39) = "11010001000"
  83. SymbolString(40) = "11000101000"
  84. SymbolString(41) = "11000100010"
  85. SymbolString(42) = "10110111000"
  86. SymbolString(43) = "10110001110"
  87. SymbolString(44) = "10001101110"
  88. SymbolString(45) = "10111011000"
  89. SymbolString(46) = "10111000110"
  90. SymbolString(47) = "10001110110"
  91. SymbolString(48) = "11101110110"
  92. SymbolString(49) = "11010001110"
  93. SymbolString(50) = "11000101110"
  94. SymbolString(51) = "11011101000"
  95. SymbolString(52) = "11011100010"
  96. SymbolString(53) = "11011101110"
  97. SymbolString(54) = "11101011000"
  98. SymbolString(55) = "11101000110"
  99. SymbolString(56) = "11100010110"
  100. SymbolString(57) = "11101101000"
  101. SymbolString(58) = "11101100010"
  102. SymbolString(59) = "11100011010"
  103. SymbolString(60) = "11101111010"
  104. SymbolString(61) = "11001000010"
  105. SymbolString(62) = "11110001010"
  106. SymbolString(63) = "10100110000"
  107. SymbolString(64) = "10100001100"
  108. SymbolString(65) = "10010110000"
  109. SymbolString(66) = "10010000110"
  110. SymbolString(67) = "10000101100"
  111. SymbolString(68) = "10000100110"
  112. SymbolString(69) = "10110010000"
  113. SymbolString(70) = "10110000100"
  114. SymbolString(71) = "10011010000"
  115. SymbolString(72) = "10011000010"
  116. SymbolString(73) = "10000110100"
  117. SymbolString(74) = "10000110010"
  118. SymbolString(75) = "11000010010"
  119. SymbolString(76) = "11001010000"
  120. SymbolString(77) = "11110111010"
  121. SymbolString(78) = "11000010100"
  122. SymbolString(79) = "10001111010"
  123. SymbolString(80) = "10100111100"
  124. SymbolString(81) = "10010111100"
  125. SymbolString(82) = "10010011110"
  126. SymbolString(83) = "10111100100"
  127. SymbolString(84) = "10011110100"
  128. SymbolString(85) = "10011110010"
  129. SymbolString(86) = "11110100100"
  130. SymbolString(87) = "11110010100"
  131. SymbolString(88) = "11110010010"
  132. SymbolString(89) = "11011011110"
  133. SymbolString(90) = "11011110110"
  134. SymbolString(91) = "11110110110"
  135. SymbolString(92) = "10101111000"
  136. SymbolString(93) = "10100011110"
  137. SymbolString(94) = "10001011110"
  138. SymbolString(95) = "10111101000"
  139. SymbolString(96) = "10111100010"
  140. SymbolString(97) = "11110101000"
  141. SymbolString(98) = "11110100010"
  142. SymbolString(99) = "10111011110"
  143. SymbolString(100) = "10111101110"
  144. SymbolString(101) = "11101011110"
  145. SymbolString(102) = "11110101110"
  146. SymbolString(103) = "11010000100"
  147. SymbolString(104) = "11010010000"
  148. SymbolString(105) = "11010011100"
  149. SymbolString(106) = "11000111010"

  150. X = X / 0.376042 'mm to pt
  151. Y = Y / 0.341 'mm to pt
  152. Height = Height / 0.341 'mm to pt

  153. If IsNumeric(Content) = True Then  ' value is numeric
  154.    i = 1 'symbol and weight index
  155.    If Len(Content) Mod 2 = 1 Then 'odd
  156.        WeightSum = SymbolValue(104) ' start-b
  157.        ContentString = ContentString + SymbolString(104)
  158.       tstr1 = Mid(Content, 1, 1)
  159.       k = 0
  160.       Do While tstr1 <> SymbolCharB(k)
  161.          k = k + 1
  162.       Loop
  163.       WeightSum = WeightSum + i * SymbolValue(k)
  164.       ContentString = ContentString + SymbolString(k)
  165.       i = i + 1
  166.       WeightSum = WeightSum + i * SymbolValue(99) 'Code-C
  167.       ContentString = ContentString + SymbolString(99) 'Code-C
  168.       Content = Right(Content, Len(Content) - 1) 'cut 1st symbol
  169.    Else 'even
  170.       WeightSum = SymbolValue(105) ' start-c
  171.       ContentString = ContentString + SymbolString(105)
  172.       i = 0
  173.    End If
  174.    
  175.    For j = 1 To Len(Content) Step 2
  176.       tstr2 = Mid(Content, j, 2)
  177.       i = i + 1
  178.       k = 0
  179.       Do While tstr2 <> SymbolCharC(k)
  180.          k = k + 1
  181.       Loop
  182.       WeightSum = WeightSum + i * SymbolValue(k)
  183.       ContentString = ContentString + SymbolString(k)
  184.    Next j
  185.    ContentString = ContentString + SymbolString(SymbolValue(WeightSum Mod 103))
  186.    ContentString = ContentString + SymbolString(106)

  187.    
  188.    Else ' alpha-numeric
  189.    WeightSum = SymbolValue(104) ' start-b
  190.    ContentString = ContentString + SymbolString(104)
  191.    i = 0 ' symbol count
  192.    For j = 1 To Len(Content) Step 1
  193.       tstr1 = Mid(Content, j, 1)
  194.       i = i + 1
  195.       k = 0
  196.       Do While tstr1 <> SymbolCharB(k)
  197.          k = k + 1
  198.       Loop
  199.       WeightSum = WeightSum + i * SymbolValue(k)
  200.       ContentString = ContentString + SymbolString(k)
  201.    Next j
  202.    ContentString = ContentString + SymbolString(SymbolValue(WeightSum Mod 103))
  203.    ContentString = ContentString + SymbolString(106)

  204. End If

  205. ContentString = ContentString + Tbar_Symbol

  206. 'Barcode drawing
  207. CurBar = 0

  208. For i = 1 To Len(ContentString)
  209.     Select Case Mid(ContentString, i, 1)
  210.     Case 0
  211.         CurBar = CurBar + 1
  212.     Case 1
  213.         CurBar = CurBar + 1
  214. ' (CurBar * LineWeight) * [B]0.9[/B] -  here is 10% overlapping :-)
  215.         With TargetSheet.Shapes.AddLine(X + (CurBar * LineWeight) * 0.9, Y, X + (CurBar * LineWeight) * 0.9, (Y + Height)).Line
  216.         .Weight = LineWeight
  217.         .ForeColor.RGB = vbBlack ' my Excel writes light-blue lines by default, so the color is forcibly switched
  218.         End With
  219.     End Select
  220. Next i

  221. End Sub
  222. </code>
复制代码
--------------------------------------------------------------------

TA的精华主题

TA的得分主题

发表于 2022-9-1 23:32 | 显示全部楼层
这个链接 的第6页 有更好的。

TA的精华主题

TA的得分主题

发表于 2022-9-2 09:16 | 显示全部楼层
如二楼所说,第六页的确实更好用,楼主发的这个代码,生成的二维码都是一条条的细线,不是太实用,我整理了两个版本的代码,需要的人可以下载

生成条形码.zip

39.82 KB, 下载次数: 456

TA的精华主题

TA的得分主题

发表于 2022-9-2 09:53 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

感谢兰色的回忆老师分享这个例子,应用非常广泛

TA的精华主题

TA的得分主题

发表于 2022-9-2 10:18 | 显示全部楼层
兰色的回忆 发表于 2022-9-2 09:16
如二楼所说,第六页的确实更好用,楼主发的这个代码,生成的二维码都是一条条的细线,不是太实用,我整理了 ...

怎么不能把这个写到函数里? 我试了 好像不行, call 这个sub 好像不成功

TA的精华主题

TA的得分主题

发表于 2022-9-2 12:09 来自手机 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2022-9-2 15:52 | 显示全部楼层
luzwales 发表于 2022-9-2 10:18
怎么不能把这个写到函数里? 我试了 好像不行, call 这个sub 好像不成功

可以的啊,下载我的附件,里面有怎么用的代码,也可以参照下面的代码,在我电脑上运行没问题的
Call mainBarCoder("Hello World!", Range("A8"), 30, 10)

TA的精华主题

TA的得分主题

发表于 2022-9-2 16:30 | 显示全部楼层
本帖最后由 luzwales 于 2022-9-2 16:35 编辑
兰色的回忆 发表于 2022-9-2 15:52
可以的啊,下载我的附件,里面有怎么用的代码,也可以参照下面的代码,在我电脑上运行没问题的
Call mai ...

我就用你的附件,不行啊!
image.png

TA的精华主题

TA的得分主题

发表于 2022-9-2 16:34 | 显示全部楼层
luzwales 发表于 2022-9-2 16:30
我就用你的附件,不行啊!

你可以把你改的文件发上来,我看看怎么回事

TA的精华主题

TA的得分主题

发表于 2022-9-2 16:41 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
image.png
就在Function 块中调用这个 怎么不行呢?
image.png
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-25 08:52 , Processed in 0.038603 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表