|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
可以copy 一个副本,看一下结果是否正确,因为没有数据难证,只能看代码猜楼主的意思表达
- Sub 计算外购件和安全库存需求___()
- Dim arr1 As Variant, arr2 As Variant, arr3 As Variant, arr4 As Variant 'Rem arr1明细表数组,arr2库存数组,arr3出库数组
- Dim brr As Variant
- Dim i As Integer
- '============================================================================================A
- arr1 = Sheet1.Range("a2:t" & Sheet1.UsedRange.Rows.Count) 'Rem 赋值外购件明细表到数组
- arr2 = Sheet2.Range("a2:f" & Sheet2.UsedRange.Rows.Count) 'Rem 赋值库存明细到数组
- arr3 = Sheet3.Range("a2:f" & Sheet3.UsedRange.Rows.Count) 'Rem 赋值出库明细到数组
- Set d = CreateObject("scripting.dictionary")
- For i = 1 To UBound(arr2) '库存明细字典
- d(arr2(i, 2) & "库存") = d(arr2(i, 2) & "库存") + arr2(i, 6) '存货编码
- Next
- For i = 1 To UBound(arr3) '出库明细字典
- d(arr3(i, 1) & arr3(i, 2) & "出库") = d(arr3(i, 1) & arr3(i, 2)) + arr3(i, 6) '项目编号+物料编号
- Next
- ReDim brr(1 To UBound(arr1), 1 To 3) '原代码一个工作表两次输出合并为一次输出
- For i = 1 To UBound(arr1)
- If d.exists(arr1(i, 4) & "库存") Then
- brr(i, 3) = d(arr1(i, 4))
- End If
- If d.exists(arr1(i, 2) & arr1(i, 4) & "出库") Then
- brr(i, 2) = d(arr1(i, 2) & arr1(i, 4) & "出库")
- End If
- If arr1(i, 17) = "" Then d(arr1(i, 4) & "总需求") = d(arr1(i, 4) & "总需求") + arr1(i, 11) - arr1(i, 13)
- Next i
- '==========总需求循环一次才能有需求数据,所以需要二次循环得出结果
- For i = 1 To UBound(arr1)
- If d.exists(arr1(i, 4) & "总需求") Then brr(i, 1) = d(arr1(i, 4) & "总需求")
- Next i
- '-------------------------------------------------------------
- Sheet1.Range("l2").Resize(UBound(arr1), 3) = brr
- '----------------------------------------------------A结束---------------------------------------------------
- '数组可以使用上面任意一个数组重新赋值而形成,因为上面的数组已经使用结束,同时也可以清除上而三个数组以释放内在占用,
- Erase arr1: Erase arr2: Erase arr3 '清除三个数组
- '============================================================================================================
- arr1 = Sheet4.Range("a2:n" & Sheet4.UsedRange.Rows.Count) 'arr1再次投入使用
- For i = 1 To UBound(arr1)
- If d.exists(arr1(i, 2) & "库存") Then '物料编码 存货
- arr1(i, 8) = d(arr1(i, 2) & "库存")
- End If
- If d.exists(arr1(i, 2) & "总需求") Then '物料编码 需求
- arr1(i, 9) = d(arr1(i, 2) & "总需求")
- End If
- Next i
- '=========================不加新数组,使用更新数组两次独列输出===================
- Sheet4.Range("h2").Resize(UBound(arr1), 1) = WorksheetFunction.Index(arr1, 0, 8)
- Sheet4.Range("i2").Resize(UBound(arr1), 1) = WorksheetFunction.Index(arr1, 0, 9)
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|