Deepseek在5月28號(hào)的時(shí)候?qū)1模型做了小幅度的升級(jí),主要是增強(qiáng)了邏輯推理能力,能生成更加準(zhǔn)確的代碼,知識(shí)庫更新到了24年6月
我迫不及待的是可以一下, 生成條碼的vba代碼竟然一次就可以使用,之前不管怎么問都不行,不得不感嘆,現(xiàn)在AI真的太強(qiáng)大,代碼直接分享給大家!
一、提示詞
下面就是我詢問的提示詞,關(guān)鍵點(diǎn)需要指出使用ActiveX控件來生成條碼,然后給出自己希望的步驟與結(jié)果。如果你的電腦中有對(duì)應(yīng)的條碼控件的話,這段代碼是無法使用的,可以通過調(diào)用API的方式來編寫VBA代碼生成了
二、代碼展示
這個(gè)代碼的作用是運(yùn)行代碼后會(huì)顯示一個(gè)提示框,輸入1生成二維碼,輸入2生成條形碼,設(shè)置對(duì)應(yīng)的條碼后。選擇對(duì)應(yīng)的區(qū)域,然后點(diǎn)擊確定即可在旁邊生成條碼
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
' 第二步:選擇數(shù)據(jù)區(qū)域
On Error Resume Next
Set rng = Application.InputBox("請選擇包含條碼數(shù)據(jù)的單元格區(qū)域", "選擇區(qū)域", Selection.Address, Type:=8)
On Error GoTo 0
If rng Is Nothing Then Exit Sub
' 設(shè)置條碼尺寸(根據(jù)類型自動(dòng)調(diào)整)
If barcodeType = 1 Then ' 二維碼
barcodeWidth = 40 ' 正方形寬度
barcodeHeight = 40 ' 正方形高度
Else ' 條形碼
barcodeWidth = 60 ' 長條形寬度
barcodeHeight = 20 ' 條形碼高度
End If
' 清除目標(biāo)區(qū)域可能存在的舊條碼(右側(cè)列)
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
' 計(jì)算條碼位置(右側(cè)列)
leftPos = cell.Offset(0, rng.Columns.count).Left + 2 ' 添加2px邊距
topPos = cell.Top + 2
' 創(chuàng)建條碼控件
Set shp = ActiveSheet.OLEObjects.Add(ClassType:="BARCODE.BarCodeCtrl.1", _
Left:=leftPos, Top:=topPos, _
Width:=barcodeWidth, Height:=barcodeHeight)
' 設(shè)置條碼屬性
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
' 調(diào)整行高以適應(yīng)條碼(高度+5px邊距)
If cell.RowHeight < barcodeHeight + 5 Then
cell.EntireRow.RowHeight = barcodeHeight + 5
End If
' 調(diào)整列寬以適應(yīng)條碼(寬度+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】調(diào)出VBA的設(shè)置窗口,之后點(diǎn)擊鼠標(biāo)右鍵找到【插入】選擇【模塊】,新建模塊后在右側(cè)【Ctrl+V】粘貼代碼。
最后只需要點(diǎn)擊【開發(fā)工具】找到【宏】運(yùn)行【GenerateBarcodeAdvanced】這個(gè)宏,根據(jù)提示來選擇數(shù)據(jù),就可以自動(dòng)的生成二維碼與條形碼
以上就是今天分享的內(nèi)容,大家可以試一下,或者直接粘貼我的代碼來使用,不得不感嘆AI現(xiàn)在真的太強(qiáng)大了!
特別聲明:以上內(nèi)容(如有圖片或視頻亦包括在內(nèi))為自媒體平臺(tái)“網(wǎng)易號(hào)”用戶上傳并發(fā)布,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。
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.