只做了两数相加的验证,两个数以上的任意组合很大比如附件的数共255记录,组合的可能是255X254X253Xn-1.....,一一验证,似乎要很长时间 不知道有没有快捷的算法,论坛里有组合的代码,也许对你有用,请搜索关键字“组合” 代码在Excel使用,如果是在Access使用可省略连接的语句 Sub AdoTs() Dim Cnn 'As New ADODB.Connection Dim Rst 'As New ADODB.Recordset Dim Sql As String Dim temArr, iArr Dim i As Integer, j As Integer, k As Integer, x As Integer Dim m As Single, n As Single Set Cnn = CreateObject("ADODB.Connection") Set Rst = CreateObject("Adodb.Recordset") '建立连接 With Cnn .Provider = "Microsoft.Jet.OLEDB.4.0" .ConnectionString = "Data Source =" & ThisWorkbook.Path & "\上网请教.mdb" .Open End With '查询小于等于3733.8的数据 Sql = "SELECT 原始数据.交易金额 From 原始数据 WHERE 原始数据.交易金额 <= 3733.80 ORDER BY 原始数据.交易金额 DESC;" Rst.Open Sql, Cnn, adOpenKeyset, adLockOptimistic '查询结果赋值数组 iArr = Rst.GetRows Rst.Close Cnn.Close Set Rst = Nothing Set Cnn = Nothing '转化为一维数组 iArr = WorksheetFunction.Index(iArr, 0) i = UBound(iArr, 1) j = 1 '查找两数相加等于3733.8的组合 Do Until i - j = 1 m = iArr(j) k = 0 If m + iArr(i) > 3773.8 Then '过滤最大值+最小值大于3733.8的数 iArr = VBA.Filter(iArr, m, False) i = UBound(iArr, 1) GoTo ATA End If Do Until i - k = 0 n = iArr(i - k) If m + n > 3773.8 Then Exit Do ElseIf m + n = 3733.8 Then x = x + 1 ReDim Preserve temArr(x) temArr(x) = m & "," & n iArr = VBA.Filter(iArr, m) iArr = VBA.Filter(iArr, n) i = UBound(iArr, 1) j = 0 Exit Do Else k = k + 1 End If Loop ATA: j = j + 1 Loop If x > 0 Then MsgBox "找到" & x & "记录" End If End Sub 写完代码,在想想觉得循环验证是多余的,走了太远的路(写循环码写顺了,一遇问题先想如何循环,呵呵) 下面的一句就可以解决问题了,建一个新查询表,把这句贴上即可. SELECT (aa.交易金额+bb.交易金额) AS 结果 FROM 原始数据 AS aa, 原始数据 AS bb WHERE (((aa.交易金额)<>[bb].[交易金额]) AND (([aa].[交易金额]+[bb].[交易金额])=3733.8));
[此贴子已经被作者于2007-4-26 15:09:10编辑过] |