查找与文本“ X”匹配的单元格,然后在VBA Excel中查找最新日期

时间:2019-04-17 13:25:33

标签: excel vba

我有3列:

B颜色:名称范围

Col E:日期范围

颜色G:美元价值

我希望代码进入B列,与我将其放置在单元格P5中的任何名称匹配,然后在E列中看到该名称的最新日期并返回G美元值。现在,我分别拥有代码,但我不知道如何将它们组合在一起:

 Function Max_Date()
   Max_Date = Application.WorksheetFunction.Max(Columns("Y"))
   MsgBox CDate(Max_Date)
 End Function

Function FindText()
 Dim rngX As Range 
  Set rngX = ActiveSheet.Range("B:B").Find(Range("P5"), lookat:=xlPart)
   If Not rngX Is Nothing Then
    MsgBox "Found at " & rngX.Address
   End If 
End Function

1 个答案:

答案 0 :(得分:0)

您可以考虑以下几点:

Public Function GetDollar(NOM) As Double

Dim RNG1 As String, RNG2 As String, RNG3 As String, LR As Double
With ActiveWorkbook.Sheets(1)
    LR = .Cells(Rows.Count, 2).End(xlUp).Row
    RNG1 = .Range(.Cells(1, 2), .Cells(LR, 2)).Address
    RNG2 = .Range(.Cells(1, 3), .Cells(LR, 3)).Address
    RNG3 = .Range(.Cells(1, 4), .Cells(LR, 4)).Address
    GetDollar = .Evaluate("=INDEX(" & RNG3 & ",MATCH(""" & NOM & """&MAX((" & RNG1 & "=""" & NOM & """)*" & RNG2 & ")," & RNG1 & "&" & RNG2 & ",0))")
End With

End Function

enter image description here

如果您要同时填充E和G列,则将需要两个UDF,因为您无法通过UDF更改其他单元格的值。