在所有运行的前面,我需要先做一个检查:生产计划中的设备是否有BOM,如果有的设备没有BOM,我不希望程序继续!
Tips:所有代码都是为目前任职公司编写,极大概率不适合其他公司,在这里发布:首先是记录;其次才是分享,望理解!
这里用两个方案,选择其中一种就可以了!
方案一:
Dim arr, brr
Dim i, j, k
Dim str
Dim d As New Dictionary 'the "Microsoft scripting runtime" need to be ticked if you want to define a new dictiory
Dim t
t = Timer
arr = Sheets("Plan").UsedRange
brr = Sheets("BOM").UsedRange
For i = 2 To UBound(brr)
str = CStr(brr(i, 1))
If Not d.Exists(str) Then
d(str) = i
End If
Next
k = 0
str = ""
For i = 2 To UBound(arr)
If Not d.Exists(CStr(arr(i, 2))) Then
str = str & CStr(arr(i, 2)) & "----" & i & "行" + vbLf
k = k + 1
End If
Next
If k > 0 Then
MsgBox "存在以下设备没有找到BOM:" & vbLf & vbLf & str, vbOKCancel, "Warning"
Exit Sub
End If
REQ.REQ
MsgBox "所有程序运行完毕,耗时" & Format(Timer - t, "#00.00") & " secords", vbOKOnly
End Sub
方案二:
Sub secondV()
Dim i, j, k
Dim arr, brr, crr()
Dim str
Dim d
Set d = CreateObject("scripting.dictionary")
arr = Sheets("Plan").UsedRange
brr = Sheets("BOM").UsedRange
For i = 2 To UBound(brr)
str = brr(i, 1)
If d(str) = "" Then
d(str) = i
End If
Next
'这样d.keys,和d.items就出来了
k = 0
For i = 2 To UBound(arr)
If Not d.Exists(arr(i, 1)) Then
k = k + 1
ReDim Preserve crr(1 To 2, 1 To k)
crr(1, k) = arr(i, 1)
crr(2, k) = i
End If
Next
Debug.Print k
'Q:i会到ubound(arr)+1才结束,那么i到达ubound(arr)+1的时候,循环主体的语句还实施吗?
str = ""
If k > 0 Then
For i = 1 To k
str = str & crr(1, i) & " : " & crr(2, i) & vbLf
Next
MsgBox str & vbLf & "以上SKU没有BOM,请检查", vbOKCancel
End If
End Sub