Hi everyone, i wrote a code for a workbook that have two worksheet´s, in the ProximoPedido (sheet) in column "A" there is a range of a values (integer number) and in colunm "B" there is a date associated, and in the ChekingList (sheet) there is the colunm "A" with the values (wich must match with "A" of ProximoPedido) and colunm "E" with dates. If value of cell A of CheckingList matches with value of "A" of ProximoPedido then search in "E" of ChekingList for next ( or closest higher) date of "B" from ProximoPedido.
Sheet: ChekingList
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:20
24 2009-10-28 10:20
3 2009-10-28 10:20
Sheet: Proximo Pedido
A B
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
i wrote a formula first with conditioning VLOOKUP and other with INDEX MATCH, but the VLOOKUP gave me the last value of all dates in CheckingList, and then i tried this code :
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
Thanks
Bookmarks