在100K行电子表格效率中查找date位置

昨天得到了一些好人的帮助。 我推断它,想问是否有人知道更有效的方法来做到这一点。 查找开始date的位置是非常有效的,因为它接近3行代码,但find结束date有点困难,因为它finddate的第一个实例,然后遍历循环,直到到达结束,这是低效的原因很明显 这些是两个独立的函数(一个用于查找date的起始行,另一个用于查找结束date的最后一行)任何人有任何想法?

注意:开始date与结束date不同…通常每隔一个月(2/1/14)(2/28/14)

有什么我的开始date和结束date是混乱的。 道歉。 dateToAnalyze是从主子接收date的参数。 第一个函数“GetStartLocations”查找在电子表格中传递给它的开始date。 保持与我最高的date一致。 开始date(2/1/14)将被传递给GetStartLocations,结束date(2/28/14)将被传递给GetEndLocations。

这些函数只是为了最终find它传递的date的行位置。 如果它没有finddate…而不是错误…它向前递增find下一个最接近的date。 这对于GetStartLocations函数非常有用,但是对于结束位置来说不是那么好,因为它可能会在所需的结束date之后递增。

Public Function GetStartLocations(dateToAnalyze As Range, masterList As Worksheet) As Long Dim finderCounter As Integer Dim finder As Range finderCounter = 0 Do Set finder = masterList.Range("A:A").Find(dateToAnalyze.Value + finderCounter, LookIn:=xlValues, LookAt:=xlWhole) finderCounter = finderCounter + 1 Loop Until Not finder Is Nothing GetStartLocations = finder.Row End Function Public Function GetEndLocations(dateToAnalyze As Range, masterList As Worksheet) As Long 'Seperate function for End location required since the find function finds the first instance of date being searched Dim finderCounter As Integer Dim finder As Range Dim location As Long finderCounter = 0 'Finds initial date in master list...If date is not found..increments upwards until it finds next nearest date sucessfully Do Set finder = masterList.Range("A:A").Find(dateToAnalyze.Value + finderCounter, LookIn:=xlValues, LookAt:=xlWhole) finderCounter = finderCounter + 1 Loop Until Not finder Is Nothing location = finder.Row Do DoEvents location = location + 1 Loop Until Range("A" & location).Value <> Range("A" & (location + 1)).Value GetEndLocations = location End Function 

如果结束date仅仅是开始date使用的最后一次出现,

 ... SearchDirection:=xlPrevious, After:=masterList.Range("A"&rows.count) 

…围绕工作表向后循环,并从底部开始查找。

查找可能不是最有效的方法。 经常阅读变体数组或使用Worksheetfunction.MATCH可以更快。
有关各种方法的优缺点的讨论,请参阅https://fastexcel.wordpress.com/2011/10/26/match-vs-find-vs-variant-array-vba-performance-shootout/