我为一个有两个工作表的工作簿编写了代码,在ProximoPedido(工作表)的“A”列中有一系列值(整数),而在colunm“B”中有一个相关的日期在ChekingList(表格)中,有一个colunm“A”,其值(必须与ProximoPedido的“A”匹配)和colunm“E”的日期。如果CheckingList的单元格A的值与ProximoPedido的“A”的值匹配,则在ChekingList的“E”中搜索来自ProximoPedido的下一个(或最接近的更高)日期“B”。
Sheet:CheckingList
一个-----------------------------------------ë
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
一个----------------------------------------乙
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
由于
答案 0 :(得分:0)
在代码中设置2个变量
DIM MatchCell as Range
Dim Targetdelta as Double
现在,循环播放Sheet:Proximo Pedido上的数据执行以下逻辑步骤