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現在真的太強大了!
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺“網易號”用戶上傳并發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.