Deepseek在5月28号的时候对R1模型做了小幅度的升级,主要是增强了逻辑推理能力,能生成更加准确的代码,知识库更新到了24年6月
我迫不及待的是可以一下, 生成条码的vba代码竟然一次就可以使用,之前不管怎么问都不行,不得不感叹,现在AI真的太强大,代码直接分享给大家!
一、提示词
下面就是我询问的提示词,关键点需要指出使用ActiveX控件来生成条码,然后给出自己希望的步骤与结果。如果你的电脑中的对应的条码控件的话,这段代码是无法使用就,可以通过调用API的方式来编写VBA代码生成了
二、代码展示
这个代码的作用是运行代码后会显示一个提示框,输入1生成二维码,输入2生成条形码,设置对应的条码后。选择对应的区域,然后点击确定即可在旁边生成条码
Sub GenerateBarcodeAdvanced()
Dim rng As Range
Dim cell As Range
Dim barcodeType As Integer
Dim shp As Object
Dim leftPos As Double, topPos As Double
Dim barcodeWidth As Double, barcodeHeight As Double
' 第一步:选择条码类型
On Error Resume Next
barcodeType = Application.InputBox("请选择条码类型:" & vbCrLf & _
"输入 1 生成二维码" & vbCrLf & _
"输入 2 生成条形码", _
"条码类型选择", Type:=1)
On Error GoTo 0
' 检查用户输入
If barcodeType < 1 Or barcodeType > 2 Then
MsgBox "操作已取消", vbInformation
Exit Sub
End If
' 第二步:选择数据区域
On Error Resume Next
Set rng = Application.InputBox("请选择包含条码数据的单元格区域", "选择区域", Selection.Address, Type:=8)
On Error GoTo 0
If rng Is Nothing Then Exit Sub
' 设置条码尺寸(根据类型自动调整)
If barcodeType = 1 Then ' 二维码
barcodeWidth = 40 ' 正方形宽度
barcodeHeight = 40 ' 正方形高度
Else ' 条形码
barcodeWidth = 60 ' 长条形宽度
barcodeHeight = 20 ' 条形码高度
End If
' 清除目标区域可能存在的旧条码(右侧列)
rng.Offset(0, rng.Columns.count).Resize(rng.Rows.count, 1).ClearContents
For Each shp In ActiveSheet.Shapes
If Not Intersect(shp.TopLeftCell, rng.Offset(0, rng.Columns.count)) Is Nothing Then
shp.Delete
End If
Next shp
' 生成条码
Application.ScreenUpdating = False
For Each cell In rng
' 计算条码位置(右侧列)
leftPos = cell.Offset(0, rng.Columns.count).Left + 2 ' 添加2px边距
topPos = cell.Top + 2
' 创建条码控件
Set shp = ActiveSheet.OLEObjects.Add(ClassType:="BARCODE.BarCodeCtrl.1", _
Left:=leftPos, Top:=topPos, _
Width:=barcodeWidth, Height:=barcodeHeight)
' 设置条码属性
With shp.Object
If barcodeType = 1 Then ' 二维码
.Style = 12 ' 二维码样式[1](@ref)
Else ' 条形码
.Style = 7 ' Code 128 条形码[1](@ref)
End If
.Value = cell.Value
.BackColor = RGB(255, 255, 255) ' 白色背景
.ForeColor = RGB(0, 0, 0) ' 黑色条码
.LineWeight = 1
End With
' 调整行高以适应条码(高度+5px边距)
If cell.RowHeight < barcodeHeight + 5 Then
cell.EntireRow.RowHeight = barcodeHeight + 5
End If
' 调整列宽以适应条码(宽度+5px边距)
If cell.Offset(0, rng.Columns.count).ColumnWidth < barcodeWidth / 6 + 1 Then
cell.Offset(0, rng.Columns.count).ColumnWidth = barcodeWidth / 6 + 1
End If
Next cell
Application.ScreenUpdating = True
MsgBox IIf(barcodeType = 1, "二维码", "条形码") & "生成完成!", vbInformation
End Sub
三、使用方法
按下快捷【ALT+F11】调出VBA的设置窗口,之后点击鼠标右键找到【插入】选择【模块】,新建模块后在右侧【Ctrl+V】粘贴代码。
最后只需要点击【开发工具】找到【宏】运行【GenerateBarcodeAdvanced】这个宏,根据提示来选择数据,就可以自动的生成二维码与条形码
以上就是今天分享的内容,大家可以试一下,或者直接粘贴我的代码来使用,不得不感叹AI现在真的太强大了!
想要提高工作效率,不想再求同事帮你解决各种Excel问题,可以了解下我的专栏,WPS用户也能使用,讲解了函数、图表、透视表、数据看板等常用功能,AI的也已经在路上了,后期都会免费更新的