我为具有两个工作表的工作簿编写了一个代码,在“A”列的 ProximoPedido(工作表)中有一个值范围(整数),在“B”列有一个关联的日期,并且在ChekingList(工作表)有列“A”和值(必须与 ProximoPedido 的“A”匹配)和列“E”和日期。如果 CheckingList 单元格 A 的值与 ProximoPedido 的“A”值匹配,则在 ChekingList 的“E”中搜索来自 ProximoPedido 的“B”的下一个(或最接近的更高)日期。
表:检查清单
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 <------ - ( 匹配!)
表: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 <----- - - - - (例子)
我首先用调节 VLOOKUP 和其他用 INDEX MATCH 编写了一个公式,但是 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
谢谢