Excel VBA:通过多个标准查找单元格值的方法

我有一个大的文件,其中包含3列和23393行的工作表中。 列A表示date,列B表示整数,第三列表示值。 我想find具有多个标准的单元格的值(第三列)。

1)第一个标准是一个date(从2008年1月1日到12/31/2009)

2)第二个标准是一个整数(它从1变到32)

谢谢

那么简单的方法呢?

 Function find(ByVal criteria1 As Date, ByVal criteria2 As Integer) As Variant For i = 2 To 300001 If Cells(i, 1).Value = criteria1 Then If Cells(i, 2).Value = criteria2 Then find = Cells(i, 3).Value Exit Function End If End If Next i find = "N/A" End Function 

你可以testing它运行一个简单的macros,如下所示:

 Sub storeFoundValues() Dim matches(2) As Variant Dim date1, date2 As Date Dim int1, int2 As Integer date1 = DateSerial(2015, 7, 15) int1 = 3 matches(1) = find(date1, int1) Cells(1, 5) = matches(1) ' test the output date2 = DateSerial(2016, 1, 10) int2 = 2 matches(2) = find(date2, int2) Cells(1, 6) = matches(2) ' test the output End Sub 

在30,000条logging的工作表中,这花了不到一秒钟的时间。

在这里输入图像说明

例如:

 =INDEX(C1:C20,SUMPRODUCT(--(A1:A20=DATEVALUE("6/17/2009"))*(B1:B20=25)*ROW(1:20))) 

在这里输入图像说明

这种方法假定只有一行符合标准。