excel检查是否在一个范围内存在一行中的单元格组合

我环顾四周,找不到它。

所以,让我们说我有这些独特的string的数组:

Dim uniques(10) As String 

现在,这些string中的每一个都在电子表格的A列中表示。

此外,我有一个整数范围从1 – 365表示B列中的唯一值。第i天

我的难题是:我想知道是否存在一行,其中A是独特的(1),而b是我,我想通过所有的独特和所有可能的我

我正在寻找一个有效的方法来做到这一点,比每一个独特的每一天循环有问题的范围。 为了logging,我的实际数据是在一年中的每一天进行计算,并且自某个事件发生以来的天数,所以我的行数可以增长到365 ^ 2,如果事件发生在一年多以前,甚至更多。

COUNTIFS似乎工作得很好。 我希望II可以去:

 numOfOccurences = Sheet1.Countifs(countrange,dayrange,uniques(1),irange,i) 

如果numOfOccurences大于0,那么我知道它存在。

还是有一个function,打破了数组中的行的范围内的行? 所以它看起来像{[A2,B2],[A3,B3],…}我可以做一些破坏,因为两列都sorting,A然后B.我只是不期望自己的function。

所有的想法都赞赏

像这样的工作。 它可能不是最优的,但是展示了如何迭代一个范围中的行:

 Sub test() Dim count As Long, i As Long Dim myRow As Range Dim uniques As Variant uniques = Array("a", "b", "c", "d", "e", "f", "g", "h", "i") 'for example i = 12 'for example For Each myRow In Range("A1:B365").Rows If myRow.Cells(1, 1).Value = uniques(1) And myRow.Cells(1, 2).Value = i Then count = count + 1 End If Next myRow MsgBoxS count End Sub 

像这样的东西应该为你工作,或者至less让你开始正确的方向。 它会查找所有出现的10个唯一值,然后从列B中获取相应的数字。我不知道如何填充您的唯一值,所以我只是做了一些事情。 Range.Find循环在很大程度上应该与你有关。

 Sub tgr() Dim rngFound As Range Dim arrUnq(1 To 10) As String Dim varUnq As Variant Dim strFirst As String Dim strResults As String arrUnq(1) = "aaa" arrUnq(2) = "bbb" arrUnq(3) = "ccc" arrUnq(4) = "ddd" arrUnq(5) = "eee" arrUnq(6) = "fff" arrUnq(7) = "ggg" arrUnq(8) = "hhh" arrUnq(9) = "iii" arrUnq(10) = "jjj" For Each varUnq In arrUnq Set rngFound = Columns("A").Find(varUnq, Cells(Rows.Count, "A"), xlValues, xlPart) If Not rngFound Is Nothing Then strFirst = rngFound.Address Do 'Here you can check what value is in column B by using rngFound.Offset(, 1).Value 'You can use this to compare to something you're looking for, or just record the result as shown in this code strResults = strResults & Chr(10) & varUnq & ": " & rngFound.Offset(, 1).Value 'Advance to the next found instance of the unique string Set rngFound = Columns("A").Find(varUnq, rngFound, xlValues, xlPart) Loop While rngFound.Address <> strFirst End If Next varUnq If Len(strResults) > 0 Then strResults = Mid(strResults, 2) 'Get rid of opening chr(10) MsgBox strResults Else MsgBox "No matches found for any of the 10 unique values" End If End Sub