|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 bingoleexuben 于 2024-5-23 11:10 编辑
=RIGHT(SUM(LEFT(A1,{0,1}+{1;3;5;7;9;11})*{9,7})) 在A1单元格输入公式
刚刚用VBA 做出批量计算校验码的代码。测试 一下
Private Sub VBA提取EAN13 随机数()
Dim number As String
Dim result As Integer, rlt As Integer, res As Integer
nRow = Sheets(1).UsedRange.Rows.Count
For j = 1 To nRow
number = Range("A" & j)
result = SumddPositions(number)
res = Sumadd(number)
rlt = result + res
Range("d" & j) = 10 - Right(rlt, 1) ' 输出结果
Next
End Sub
Function SumddPositions(number As String) As Integer
Dim sum As Integer
Dim i As Integer
Dim digit As Integer
sum = 0
For i = 1 To Len(number)
digit = Mid(number, i, 1)
If i Mod 2 = 1 Then ' 判断是否为奇数位
sum = sum + digit
End If
Next i
SumddPositions = sum
End Function
Function Sumadd(number As String) As Integer
Dim su As Integer
Dim k As Integer
Dim di As Integer
su = 0
For k = 1 To Len(number)
di = Mid(number, k, 1) * 3
If k Mod 2 = 0 Then ' 判断是否为奇数位
su = su + di
End If
Next k
Sumadd = su
End Function
补充内容 (2024-5-28 13:55):
Private Sub 计算EAN()
Dim number As String
Dim result As Integer, rlt As Integer, res As Integer
nRow = Sheets(1).UsedRange.Rows.Count
Range("D2:E" & nRow).ClearContents
... |
|