- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
本帖最後由 GBKEE 於 2014-4-10 19:06 編輯
回復 2# melvinhsu
試試看- Option Explicit
- Sub testing3()
- Dim i As Integer, D1 As Object, D2 As Object, Rng As Range
- Set D1 = CreateObject("SCRIPTING.DICTIONARY") '字典物件
- Set D2 = CreateObject("SCRIPTING.DICTIONARY") '字典物件
- i = 2 '第2列開始
-
- Do While Cells(i, "G") <> "" '執行迴圈條件 G欄<>""
- With Cells(i, "G") 'G欄i列 的物件
- If Not D1.Exists(.Value) Then '字典的key不存在
- D1(.Value) = Cells(i, "J") '庫存數
- D2(.Value) = 0 '出貨數總數
- End If
- If (D1(.Value) - D2(.Value)) >= Cells(i, "H") Then '庫存數-出貨數總數>=訂單數
- Cells(i, "I") = Cells(i, "H") '訂單實際出貨數
- D2(.Value) = D2(.Value) + Cells(i, "I") '訂單實際出貨數的加總
- ElseIf (D1(.Value) - D2(.Value)) > 0 And (D1(.Value) - D2(.Value)) < Cells(i, "H") Then
- '庫存數-出貨數總數 > 0 '庫存數-出貨數總數 > 訂單數
- Cells(i, "I") = D1(.Value) - D2(.Value) '訂單實際出貨數=庫存數-出貨數總數
- D2(.Value) = D2(.Value) + Cells(i, "I") '出貨數總數=出貨數總數+訂單實際出貨數
- ElseIf D1(.Value) = D2(.Value) Then '無貨可出:庫存數=出貨數總數
- Cells(i, "I") = ""
- If Not Rng Is Nothing Then
- Set Rng = Union(Rng, Range("F" & i & ":J" & i))
- Else
- Set Rng = Range("F" & i & ":J" & i)
- End If
- End If
- End With
- i = i + 1
- Loop
- If Not Rng Is Nothing Then Rng.Select
- End Sub
複製代碼 |
|