【问题标题】:vba that search for a value if it matches find closest date如果值匹配,则搜索值的 vba 查找最近的日期
【发布时间】:2013-05-08 18:41:06
【问题描述】:

我为具有两个工作表的工作簿编写了一个代码,在“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 中所有日期的最后一个值,然后我尝试了这段代码: 子 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

谢谢

【问题讨论】:

    标签: vba date find next


    【解决方案1】:

    在您的代码中设置 2 个变量

    1. 第一个将跟踪单元格位置DIM MatchCell as Range
    2. 第二个将跟踪源单元格和 匹配单元Dim Targetdelta as Double

    现在,遍历 Sheet 上的数据:Proximo Pedido 执行以下逻辑步骤

    1. 计算当前单元格上的日期与 CheckList 中的日期之间的绝对差值
    2. 如果 delta 小于 Targetdelta 的当前值
    3. THEN 将 delta 更新为新值并将当前地址记录在 MatchCell 变量中
    4. 继续循环
    5. 循环之后,您知道哪个单元格最近 - 它存储在 MatchCell 变量中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 2017-02-23
      • 2011-02-21
      相关资源
      最近更新 更多