如果在Excel中提供了一列数据,例如:
2
5
8
10
3
6
是否可以检查列以查找符合特定条件的所有组合?在此示例中,当这些值的任何组合的总和等于8时。
它应该找到的值是
2
5
8
3
6
2 + 6,5 + 3,8均等于8
据我了解,我基本上要求检查以下内容
if 2 = 8 or
if 5 = 8 or
if 8 = 8 or
if 10 = 8 or
if 3 = 8 or
if 6 = 8 or
if 2+5 = 8 or
if 2+8 = 8 or
if 2+10 = 8 or
if 2+3 = 8 or
if 2+6 = 8 or
etc
我只使用简单的数字来试一试。我很确定这是不可能的。
答案 0 :(得分:1)
尝试以下代码。您必须在A列中输入要匹配的值,结果将显示在B列中。我能够实现双向匹配和三向匹配。如果你愿意,它可以做到5路匹配后会变得更复杂。
Sub Testing()
Dim RowNumber As Double
Dim Temp1 As Double
Dim Temp2 As Double
Dim Temp3 As Double
Dim Result As Double
Dim MatchCount As Double
'Value to be searched
Result = Application.InputBox("Please insert a Number", "Combi Calculator", "", , , , , 1)
'get the last row
RowNumber = Sheet1.Range("A1048576").End(xlUp).Row
'set matchcount to
MatchCount = 1
'Two way match
For Temp1 = 2 To RowNumber
For Temp2 = 3 To RowNumber
If Cells(Temp1, 2) = "" And Cells(Temp2, 2) = "" Then
If Cells(Temp1, 1) + Cells(Temp2, 1) = Result Then
Cells(Temp1, 2) = MatchCount
Cells(Temp2, 2) = MatchCount
MatchCount = MatchCount + 1
End If
End If
Next
Next
'Three way match
For Temp1 = 2 To RowNumber
For Temp2 = 3 To RowNumber
For Temp3 = 4 To RowNumber
If Cells(Temp1, 2) = "" And Cells(Temp2, 2) = "" And Cells(Temp3, 2) = "" And Temp1 <> Temp2 And Temp2 <> Temp3 And Temp1 <> Temp3 Then
If Cells(Temp1, 1) + Cells(Temp2, 1) + Cells(Temp3, 1) = Result Then
Cells(Temp1, 2) = MatchCount
Cells(Temp2, 2) = MatchCount
Cells(Temp3, 2) = MatchCount
MatchCount = MatchCount + 1
End If
End If
Next
Next
Next
End Sub
我不知道如何将excel文件作为附件发送,因为我是这个页面的新手。
同时让我再研究一下时间话题。这一直是我想要工作的领域(即逻辑)。答案 1 :(得分:1)
使用COUNTIF
进行双向匹配非常简单。如果您的数字范围在A列中,并且您希望在总和为72的范围内找到两个数字,请在B1中输入此公式并将其复制到B20:
=COUNTIF($A$1:$A$20,72-A1)