請測試看看,謝謝
Sub test()
Dim Arr, xD, xD1, xD2, xD3, xD4, T$, i&, QA, QB
Set xD = CreateObject("Scripting.Dictionary") '入庫合計
Set xD1 = CreateObject("Scripting.Dictionary") '公司總需求
Set xD2 = CreateObject("Scripting.Dictionary") 'A倉
Set xD3 = CreateObject("Scripting.Dictionary") 'B倉
Set xD4 = CreateObject("Scripting.Dictionary") '總出貨
TM = Timer
With Sheets("入庫明細")
Arr = .Range(.[r1], .[o65536].End(3))
For i = 2 To UBound(Arr)
T = Arr(i, 1): xD(T) = xD(T) + Arr(i, 4) '入庫合計
Next
End With
With Sheets("全機種BOM")
Arr = .Range(.[z1], .[p65536].End(3))
For i = 2 To UBound(Arr)
T = Arr(i, 1): xD1(T) = xD1(T) + Arr(i, 11) '公司總需求
Next
End With
With Sheets("A需求")
Arr = .Range(.[h1], .[a65536].End(3))
For i = 4 To UBound(Arr)
T = Arr(i, 1): xD2(T) = xD2(T) + Arr(i, 8) 'A倉
Next
End With
With Sheets("B需求")
Arr = .Range(.[h1], .[a65536].End(3))
For i = 4 To UBound(Arr)
T = Arr(i, 1): xD3(T) = xD3(T) + Arr(i, 8) 'B倉
Next
End With
With Sheets("指圖明細")
Arr = .Range(.[L1], .[f65536].End(3))
For i = 4 To UBound(Arr)
T = Arr(i, 1): xD4(T) = xD4(T) + Arr(i, 7) '總出貨
Next
End With
With Sheets("倉庫庫存")
Arr = .Range(.[m3], .[a65536].End(3))
For i = 2 To UBound(Arr)
T = Arr(i, 1)
QA = Arr(i, 4) + Arr(i, 5) ' ''倉庫庫存
QB = Arr(i, 11) + Arr(i, 12)
Arr(i, 5) = xD(T) '入庫合計
Arr(i, 13) = xD4(T) '總出貨
Arr(i, 3) = xD1(T) '總需求
Arr(i, 8) = QA - QB - xD2(T) - xD3(T) - xD4(T) '公司倉
Arr(i, 9) = xD3(T) 'B倉
Arr(i, 10) = xD2(T) 'A倉
Arr(i, 7) = QA - QB - xD4(T) '總數
Next
.[a3].Resize(UBound(Arr), 13) = Arr
End With
MsgBox "共耗時:" & Timer - TM & " 秒"
End Sub作者: Andy2483 時間: 2022-9-15 08:37
是這樣嗎? 請測試看看,謝謝
With Sheets("入庫明細")
Arr = .Range(.[S1], .[o65536].End(3))
For i = 2 To UBound(Arr)
If Arr(i, 5) = "公司倉" Then '是公司倉-->貨物編號累加R欄的Qty
T = Arr(i, 1): xD(T) = xD(T) + Arr(i, 4) '入庫合計
End If
Next
End With作者: Andy2483 時間: 2022-9-16 08:28
Sub 倉庫庫存()
Dim Arr, xD, xD1, xD2, xD3, xD4, xD5, xD6, xD7, T$, i&, QA, QB
Set xD = CreateObject("Scripting.Dictionary") '入庫合計
Set xD1 = CreateObject("Scripting.Dictionary") '公司總需求
Set xD2 = CreateObject("Scripting.Dictionary") 'A倉
Set xD3 = CreateObject("Scripting.Dictionary") 'B倉
Set xD4 = CreateObject("Scripting.Dictionary") '總出貨
Set xD5 = CreateObject("Scripting.Dictionary") '盤點數'
Set xD6 = CreateObject("Scripting.Dictionary") '退庫
Set xD7 = CreateObject("Scripting.Dictionary") '廢料
TM = Timer
With Sheets("入庫明細")
Arr = .Range(.[r1], .[o65536].End(3))
For i = 2 To UBound(Arr)
T = Arr(i, 1): xD(T) = xD(T) + Arr(i, 4) '入庫合計
Next
End With
With Sheets("全機種BOM")
Arr = .Range(.[Z1], .[p65536].End(3))
For i = 2 To UBound(Arr)
T = Arr(i, 1): xD1(T) = xD1(T) + Arr(i, 11) '公司總需求
Next
End With
With Sheets("A需求")
Arr = .Range(.[h1], .[A65536].End(3))
For i = 4 To UBound(Arr)
T = Arr(i, 1): xD2(T) = xD2(T) + Arr(i, 8) 'A倉
Next
End With
With Sheets("B需求")
Arr = .Range(.[h1], .[A65536].End(3))
For i = 4 To UBound(Arr)
T = Arr(i, 1): xD3(T) = xD3(T) + Arr(i, 8) 'B倉
Next
End With
With Sheets("指圖明細")
Arr = .Range(.[L1], .[f65536].End(3))
For i = 4 To UBound(Arr)
T = Arr(i, 1): xD4(T) = xD4(T) + Arr(i, 7) '總出貨
Next
End With
With Sheets("公司盤點")
Arr = .Range(.[G1], .[A65536].End(3))
For i = 4 To UBound(Arr)
T = Arr(i, 1): xD5(T) = xD5(T) + Arr(i, 7) '公司盤點
Next
End With
With Sheets("退庫")
Arr = .Range(.[C1], .[A65536].End(3))
For i = 3 To UBound(Arr)
T = Arr(i, 1): xD6(T) = xD6(T) + Arr(i, 3) 'B倉
Next
End With
With Sheets("廢料倉")
Arr = .Range(.[C1], .[A65536].End(3))
For i = 3 To UBound(Arr)
T = Arr(i, 1): xD7(T) = xD7(T) + Arr(i, 3) 'B倉
Next
End With
If Arr(i, 3) > 0 Then
XA = xD5(T) + xD(T) - xD6(T) - xD7(T) - Arr(i, 3)
If XA >= 0 Then XA = "0"
Else
XA = "0"
End If
If xD(T) = 0 Then Arr(i, 5) = "0"
If xD5(T) = 0 Then Arr(i, 4) = "0"
If xD1(T) = 0 Then Arr(i, 3) = "0"
If xD4(T) = 0 Then Arr(i, 13) = "0"
Arr(i, 6) = XA
Next
.[a3].Resize(UBound(Arr), 13) = Arr
End With
'MsgBox "共耗時:" & Timer - TM & " 秒"
End Sub作者: Andy2483 時間: 2022-9-17 16:58
謝謝 兩位前輩
今天習得
1.倒入字典迴圈化
2.預設2條件吻合才加總
Option Explicit
Sub 倉庫庫存_20220917()
Application.ScreenUpdating = False
Dim x&, i&, 值(1 To 17) As Long, QA, QB, T, S, Srr, Arr, Ac, xR, C
For i = 1 To UBound(S)
'↑設外順迴圈從 1 到 S陣列的最後一個 7
Set Rq1s = Srr(特rr(i)(3))(1, 特rr(i)(4))
Set Rq1n = Srr(特rr(i)(3))(Rs, 特rr(i)(4)).End(3)
Brr = Srr(特rr(i)(3)).Range(Rq1s, Rq1n)
'↑令Brr是陣列 將條件1的儲存格值資料倒入,當被搜尋的關鍵字
Set Rq2s = Srr(特rr(i)(7))(1, 特rr(i)(8))
Set Rq2n = Srr(特rr(i)(7))(Rq1n.Row, 特rr(i)(8))
Drr = Srr(特rr(i)(7)).Range(Rq2s, Rq2n)
'↑令Drr是陣列 將條件2的儲存格值資料倒入,當被搜尋的關鍵字
Set Ras = Srr(特rr(i)(1))(1, 特rr(i)(2))
Set Ran = Srr(特rr(i)(1))(Rq1n.Row, 特rr(i)(2))
Crr = Srr(特rr(i)(1)).Range(Ras, Ran)
'↑令Crr是陣列 結果儲存格值資料倒入
For x = 1 To UBound(Brr)
'↑設內順迴圈從 1 到 第1條件的最後個
B = Brr(x, 1)
'↑貨品編號
If InStr(Drr(x, 1), 特rr(i)(9)) Or Drr(x, 1) & 特rr(i)(9) = "" Then
'↑如果第二條件成立 或
'第二條件的關鍵字欄格值與 特rr(i)第9個ITEM 組合的字串是空字元
'因為 如果沒有第二條件判斷的工作表資料!也要創立字典供後續引用
''此範例CU欄一定是空格,與特rr(i)(9) = ""組合字串也是空格!
'所以第二條件一定會成立!
'因為第一條件就是 貨品編號 是字典一定會納入
Trr(i)(B) = Trr(i)(B) + Crr(x, 1)
'↑條件成立就把 貨品編號當key去除重複,結果儲存格值累加當item
End If
Next
Next
For i = 1 To Ac - 3
'↑設順迴圈將資料帶入或計算後再帶入!
xR = Arr(i, 1)
Arr(i, 4) = Trr(7)(xR)
Arr(i, 5) = Trr(3)(xR)
Arr(i, 6) = Trr(1)(xR) + Trr(2)(xR)
Arr(i, 8) = Trr(5)(xR) + Trr(6)(xR)
If Trr(3)(xR) = 0 Then Arr(i, 5) = 0
If Trr(7)(xR) = 0 Then Arr(i, 4) = 0
If Trr(1)(xR) + Trr(2)(xR) = 0 Then Arr(i, 6) = 0
If Trr(5)(xR) + Trr(6)(xR) = 0 Then Arr(i, 8) = 0
Arr(i, 7) = Trr(5)(xR) + Trr(6)(xR) + Trr(1)(xR) + Trr(2)(xR) - Trr(3)(xR)
If Arr(i, 7) >= 0 Then Arr(i, 7) = 0
Next i
Sheets(S(0)).[A4].Resize(UBound(Arr), 8) = Arr
MsgBox "共耗時:" & Timer - T & " 秒"
End Sub作者: singo1232001 時間: 2022-10-21 17:55
Sub 倉庫庫存()
Set d = CreateObject("Scripting.Dictionary")
r = Sheets("材料表").Cells(Rows.Count, 1).End(3).Row
For Each Z In Sheets("材料表").Range("a2:a" & r)
d(Z.Value) = Z.Row - 1
Next
'1 '2 '3 8 7 9 10 11
sA = Split("全機種BOM,公司盤點,入庫明細,A需求,B需求,退庫,廢料倉,指圖明細", ",")
sB = Split("p:z,a:g,o:r,a:h,a:h,a:c,a:c,f:l", ",")
ReDim Ar(1 To d.Count, 1 To 11) As Double
For i = 0 To UBound(sA) '放資料
Set s = Sheets(sA(i))
sC = Split(sB(i), ":")
r = s.Cells(Rows.Count, sC(0)).End(3).Row
c = Split("1,2,3,8,7,9,10,11", ",")(i)
For j = 1 To r
If d.exists(s.Cells(j, sC(0)).Value) Then
Ar(d(s.Cells(j, sC(0)).Value), c) = Ar(d(s.Cells(j, sC(0)).Value), c) + s.Cells(j, sC(1)).Value
End If
Next
Next
For i = 1 To UBound(Ar) '計算一下
a23910 = Ar(i, 3) + Ar(i, 2) - Ar(i, 9) - Ar(i, 10)
Ar(i, 6) = a23910 - Ar(i, 11) - Ar(i, 8) - Ar(i, 7)
Ar(i, 5) = a23910 - Ar(i, 11)
Ar(i, 4) = a23910 - Ar(i, 1)
If Ar(i, 4) >= 0 Then Ar(i, 4) = 0
Next
Sheets("倉庫庫存").Range("c4").Resize(UBound(Ar) - 1, 11) = Ar
End Sub作者: singo1232001 時間: 2022-10-21 18:24
Sub 倉庫庫存2()
Set d = CreateObject("Scripting.Dictionary")
Set s = Sheets("材料表")
For Each Z In s.Range("a2:a" & s.Cells(Rows.Count, 1).End(3).Row)
d(Z.Value) = Z.Row - 1: Next
'1 '2 '3 8 7 9 10 11
sA = Split("全機種BOM,公司盤點,入庫明細,A需求,B需求,退庫,廢料倉,指圖明細", ",")
sB = Split("p:z,a:g,o:r,a:h,a:h,a:c,a:c,f:l", ",")
ReDim Ar(1 To d.Count, 1 To 11) As Double
For i = 0 To UBound(sA) '放資料
Set s = Sheets(sA(i))
sC = Split(sB(i), ":")
Lr = s.Cells(Rows.Count, sC(0)).End(3).Row
c = Split("1,2,3,8,7,9,10,11", ",")(i)
For j = 1 To Lr
r = s.Cells(j, sC(0)).Value
If d.exists(r) Then Ar(d(r), c) = Ar(d(r), c) + s.Cells(j, sC(1)).Value
Next
Next
For i = 1 To UBound(Ar) '計算一下
a23910 = Ar(i, 3) + Ar(i, 2) - Ar(i, 9) - Ar(i, 10)
Ar(i, 4) = a23910 - Ar(i, 1): If Ar(i, 4) >= 0 Then Ar(i, 4) = 0
Ar(i, 5) = a23910 - Ar(i, 11)
Ar(i, 6) = Ar(i, 5) - Ar(i, 8) - Ar(i, 7)
Next
Sheets("倉庫庫存").Range("c4").Resize(UBound(Ar) - 1, 11) = Ar
End Sub作者: Andy2483 時間: 2022-10-25 12:40