返回列表 上一主題 發帖

[發問] 前輩關於數列減去的寫法

本帖最後由 GBKEE 於 2014-4-10 19:06 編輯

回復 2# melvinhsu
試試看
  1. Option Explicit
  2. Sub testing3()
  3.     Dim i As Integer, D1 As Object, D2 As Object, Rng As Range
  4.     Set D1 = CreateObject("SCRIPTING.DICTIONARY")  '字典物件
  5.     Set D2 = CreateObject("SCRIPTING.DICTIONARY")  '字典物件
  6.     i = 2                                          '第2列開始
  7.    
  8.     Do While Cells(i, "G") <> ""                   '執行迴圈條件 G欄<>""
  9.         With Cells(i, "G")                         'G欄i列 的物件
  10.             If Not D1.Exists(.Value) Then          '字典的key不存在
  11.                 D1(.Value) = Cells(i, "J")         '庫存數
  12.                 D2(.Value) = 0                     '出貨數總數
  13.             End If
  14.             If (D1(.Value) - D2(.Value)) >= Cells(i, "H") Then '庫存數-出貨數總數>=訂單數
  15.                 Cells(i, "I") = Cells(i, "H")           '訂單實際出貨數
  16.                 D2(.Value) = D2(.Value) + Cells(i, "I") '訂單實際出貨數的加總
  17.             ElseIf (D1(.Value) - D2(.Value)) > 0 And (D1(.Value) - D2(.Value)) < Cells(i, "H") Then
  18.                  '庫存數-出貨數總數 > 0                 '庫存數-出貨數總數 > 訂單數
  19.                 Cells(i, "I") = D1(.Value) - D2(.Value) '訂單實際出貨數=庫存數-出貨數總數
  20.                 D2(.Value) = D2(.Value) + Cells(i, "I") '出貨數總數=出貨數總數+訂單實際出貨數
  21.             ElseIf D1(.Value) = D2(.Value) Then         '無貨可出:庫存數=出貨數總數
  22.                 Cells(i, "I") = ""
  23.                 If Not Rng Is Nothing Then
  24.                     Set Rng = Union(Rng, Range("F" & i & ":J" & i))
  25.                 Else
  26.                     Set Rng = Range("F" & i & ":J" & i)
  27.                 End If
  28.             End If
  29.         End With
  30.         i = i + 1
  31.     Loop
  32.    If Not Rng Is Nothing Then Rng.Select
  33. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 自己害自己,莫過於亂發脾氣。
返回列表 上一主題