2013-05-14 81 views
0

我寫了一個工作簿的代碼,有兩個工作表,在ProximoPedido(表)列「A」中有一個值的範圍(整數)和colunm「B」中有一個相關日期,並且在ChekingList(表單)中存在具有值(必須與ProximoPedido的「A」匹配)的柱狀「A」和具有日期的柱狀「E」。如果CheckingList的單元格A的值與ProximoPedido的「A」的值匹配,則在ChekingList的「E」中搜索ProximoPedido的下一個(或最接近的更高)日期「B」。vba如果它匹配找到最接近的日期

表:CheckingList

A -------------------------------------- --- E

1 ---------------------------------- 2009-10-30 12: 00

3 --------------------------------- 2009-10-29 13:00

2 --------------------------------- 2009-10-29 12:20

50-- ------------------------------ 2009-10-19 10:2 0

24 -------------------------------- 2009-10-28 10:20

3 ---------------------------------- 2009-10-28 10:20 < ------ - ( 比賽!)

表:關閉請求

的------------------------------------乙----

4 ---------------------------------- 2009-10-28 10 :20

20 --------------------------------- 2009-10-29 13:00

---------------------------------- 3 2009-10-19 15:20

24 --------------------------------- 2009-10-29 13點40

3 ----------------------------------- 2009-10-27 13: < ------------ 20(例如)

公式我寫的第一和調理與VLOOKUP與其他折射率匹配,但VLOOKUP給了我checkinglist所有日期的最後一個值,然後,我想這代碼:Sub TempoTotal1()在你的代碼

Dim CheckingList As Worksheet 
    Dim ProximPedido As Worksheet 
    Dim tear1 As Range 
    Dim inicio As Range 
    Dim tear2 As Range 
    Dim saida As Range 
    Dim diferença As Range 
    Dim cell1 As Range 
    Dim cell2 As Range 
    Dim i As Integer 





Set tear1 = Worksheets("CheckingList").Range("a2").CurrentRegion 
Set inicio = Worksheets("CheckingList").Range("e2").CurrentRegion 
Set tear2 = Worksheets("ProximoPedido").Range("a1").CurrentRegion 
Set saida = Worksheets("ProximoPedido").Range("b2").CurrentRegion 
Set diferença = Worksheets("ProximoPedido").Range("c2").CurrentRegion 



On Error Resume Next 

For Each cell1 In tear1 
If tear1.Cells.Value = tear2.Cells.Value Then 

For Each cell2 In inicio 


If tear2.Cells.Value > saida.Cells.Value Then 
diferença.Cells.Value = inicio.Cells.Value - saida.Cells.Value 

End If 
Exit For 
Next cell2 


End If 
Exit For 
Next cell1 








End Sub 

感謝

回答

0

設置2個變量

  1. 第一將跟蹤小區位置DIM MatchCell as Range
  2. 第二將跟蹤源小區和 匹配細胞之間的增量Dim Targetdelta as Double

現在,循環通過上表的日期:請求下一頁表演以下邏輯步驟

  1. 計算在當前單元格的日期和日期
  2. 0123之間,並在清單中的絕對δ
  3. IF闕增量小於Targetdelta
  4. 的當前值
  5. 然後更新增量爲新值,並記錄在MatchCell可變
  6. 繼續循環
  7. 循環後的當前地址,你知道哪些細胞是最接近 - 其存儲在變量MatchCell