这个比较简单,就是将两个表格的信息组合起来。
Tips:所有代码都是为目前任职公司编写,极大概率不适合其他公司,在这里发布:首先是记录;其次才是分享,望理解!
效果图:
思维导图:
代码:
Sub SIOP()Dim i, j, k, s, m, n, a
Dim arr, brr, crr(), drr(), err()
Dim d As New DictionaryOn Error Resume Next
arr = Sheets("PO").UsedRange
brr = Sheets("Net").UsedRange
'我们觉得横向会比较好
'先把负数的挑出来
m = 0
For i = 2 To UBound(brr)d(CStr(brr(i, 1))) = is = 0For k = 7 To UBound(brr, 2)s = s + brr(i, k)NextIf s < 0 Thenm = m + 1ReDim Preserve crr(1 To UBound(brr, 2), 1 To m)For j = 1 To UBound(brr, 2)'把这一行的数据存入crr中,vertical,这里甚至不需要字典dcrr(j, m) = brr(i, j)NextEnd If
Next'Sheets("test").[a1].Resize(UBound(crr, 2), UBound(crr)) = Application.Transpose(crr)'接下来我们确定新数组需要多少行(在m的基础上加多少行n = 0
For i = 2 To UBound(arr)If d.Exists(CStr(arr(i, 1))) Thenn = n + 1End If
Next
'Debug.Print nReDim drr(1 To m, 1 To UBound(crr))For i = 1 To UBound(crr)For j = 1 To UBound(crr, 2)drr(j, i) = crr(i, j)Next
NextSheets("test").[a:a].NumberFormatLocal = "@"
Sheets("test").[a1].Resize(UBound(drr), UBound(drr, 2)) = drrReDim err(1 To m + n, 1 To UBound(drr, 2) + 7)a = 0 'a用来控制行数
For i = 1 To UBound(drr)'查找dd字典,分两种情况,一种是找不到not exists,一种是找到了,又分只有一行或者多行处理s = 0 '用来判断是否找到了PO'这里也不需要字典dd(被我删除了),因为字典不能确定到某一行For j = 1 To UBound(arr)If drr(i, 1) = arr(j, 1) Thens = s + 1a = a + 1If s = 1 ThenFor k = 1 To UBound(drr, 2)err(a, k) = drr(i, k)NextFor k = 1 To UBound(arr, 2)err(a, UBound(drr, 2) + k) = arr(j, k)NextElseFor k = 1 To UBound(arr, 2)err(a, UBound(drr, 2) + k) = arr(j, k)NextEnd IfEnd IfNextIf s = 0 Thena = a + 1For k = 1 To UBound(drr, 2)err(a, k) = drr(i, k)NextEnd If
NextSheets("SIOP").[a:a].NumberFormatLocal = "@"
Sheets("SIOP").[a1].Resize(UBound(err), UBound(err, 2)) = err