返回列表 上一主題 發帖

[發問] 求助~排程填入日期

回復 8# adam2010
  1. Sub Test()
  2. Dim rng As Range
  3. Dim s1 As Long, s2 As Long
  4. Dim cindex As Long

  5. With Sheets("出貨日")
  6.     Set rng = .Range(.[A1], .[A1].End(xlToRight).End(xlDown).Offset(, -1))  '[出貨日]資料範圍
  7. End With

  8. For Each c In Sheets("交期").Range("E2:E" & Sheets("交期").[A1].End(xlDown).Row)   '[交期]資料填入範圍
  9.     If c.Offset(, -4).Value <> c.Offset(-1, -4).Value Then
  10.         cindex = 1
  11.         s1 = 0  '累積至前一批數量
  12.         s2 = 0  '累積出貨需求數量
  13.     Else
  14.         s1 = s1 + c.Offset(-1, -1).Value
  15.     End If
  16.    
  17.     Do While s2 <= s1
  18.         cindex = cindex + 1
  19.         If Application.IsError(Application.VLookup(c.Offset(, -4).Value, rng, cindex, False)) Then
  20.             Exit Do
  21.         Else
  22.             s2 = s2 + Application.VLookup(c.Offset(, -4).Value, rng, cindex, False)
  23.         End If
  24.     Loop
  25.    
  26.     If s2 <= s1 Then
  27.         c.Value = "NA"
  28.     Else
  29.         c.Value = rng.Cells(1, cindex).Value
  30.     End If
  31. Next
  32. Set rng = Nothing
  33. End Sub
複製代碼

TOP

感謝Dino大,Hsieh大以及stillfish00大的協助,
測試無誤,解決我近幾週的困擾,太感動了,謝謝!
Adam

TOP

回復 10# adam2010
Lookup函數並不適用
  1. Sub ex()
  2. Dim Ar(), Ay(), C As Range, rng As Range
  3. Set d = CreateObject("Scripting.Dictionary") '數量
  4. Set d1 = CreateObject("Scripting.Dictionary") '日期
  5. With Sheets("出貨日")
  6. For Each a In .Range(.[A2], .[A2].End(xlDown)) '物料
  7.   Set rng = a.EntireRow.SpecialCells(xlCellTypeConstants, xlNumbers) '應交貨數量
  8.      For Each C In rng
  9.         cnt = cnt + C
  10.         ReDim Preserve Ar(s)
  11.         ReDim Preserve Ay(s)
  12.         Ar(s) = cnt
  13.         Ay(s) = .Cells(1, C.Column).Value
  14.         s = s + 1
  15.      Next
  16.      d(a.Value) = Ar
  17.      d1(a.Value) = Ay
  18.      Erase Ar: Erase Ay: s = 0: cnt = 0
  19. Next
  20. End With
  21. With Sheets("交期")
  22.   For Each a In .Range(.[A2], .[A2].End(xlDown))
  23.      cnt = cnt + a.Offset(, 3)
  24.      If cnt <= Application.Max(d(a.Value)) Or cnt - a.Offset(, 3) < Application.Max(d(a.Value)) Then
  25.      i = 0
  26.      Do Until cnt - a.Offset(, 3) < d(a.Value)(i) Or i = UBound(d(a.Value))  '找到陣列中比已交貨數量還大的應交貨數量
  27.      n = d(a.Value)(i)
  28.        i = i + 1
  29.      Loop
  30.      a.Offset(, 4) = d1(a.Value)(i)
  31.      Else
  32.      a.Offset(, 4) = "NA"
  33.      End If
  34.      If a <> a.Offset(1) Then cnt = 0
  35.   Next
  36. End With
  37. End Sub
複製代碼
學海無涯_不恥下問

TOP

Dear  Hsieh大,感謝您的回覆,測試無誤
目前測試您跟stillfish00所提供的均正確,我再研究看看兩者的差異,謝謝!
Adam

TOP

        靜思自在 : 滴水成河。粒米成蘿,勿輕己靈,勿以善小而不為。
返回列表 上一主題