I had posted this a while back, since then I got some results with a shortened version. This version is not giving me desired results.
It should find four orders that total to 126.63. I separated the data in to days so run time was reasonable.
It's not finding any combinations of four for that total. I want to verify that the code is correct.
Whole spreadsheet attached.
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 11/19/2009 by orders
'
'
Dim finish As Integer, start As Integer
Dim firstval, secondval As Double
Dim counter As Integer, innerstart1 As Integer, innerctr1 As Integer
Dim endfin As Integer
Dim tot_val As Double
Dim innerctr2 As Integer, innerctr3 As Integer
Dim innerstart2 As Integer, innerstart3 As Integer
Dim innerctr4 As Integer, innerstart4 As Integer
Dim third_val As Double, fourth_val As Double
Dim outerctr As Integer
Dim name_zip1 As String, name_zip2 As String, name_zip3 As String
Dim name_zip4 As String, name_zip5 As String
Dim date_k1 As String, date_k2 As String, date_k3 As String
Dim date_k4 As String, date_k5 As String
Dim x As Integer
Dim thisRow As Integer, prevRow As Integer, myCol As Integer
thisRow = 0
prevRow = 0
Selection.SpecialCells(xlCellTypeLastCell).Select
endfin = ActiveCell.Row - 1
start = 0
innerstart1 = start + 1
innerstart2 = start + 2
innderstart3 = start + 3
innerstart4 = start + 4
Range("P3").Select
For counter = start To endfin - 4
firstval = ActiveCell.Value
thisRow = ActiveCell.Row
ActiveCell.Offset(0, -6).Range("A1").Activate
name_zip1 = ActiveCell.Value
ActiveCell.Offset(0, -6).Range("A1").Activate
date_k1 = ActiveCell.Value
ActiveCell.Offset(0, 12).Range("A1").Activate
For innerctr2 = innerstart2 To endfin - 2
ActiveCell.Offset(1, 0).Range("A1").Activate
thirdval = ActiveCell.Value
ActiveCell.Offset(0, -6).Range("A1").Activate
name_zip3 = ActiveCell.Value
ActiveCell.Offset(0, -6).Range("A1").Activate
date_k3 = ActiveCell.Value
ActiveCell.Offset(0, 12).Range("A1").Activate
For innerctr3 = innerstart3 To endfin - 2
ActiveCell.Offset(1, 0).Range("A1").Activate
fourthval = ActiveCell.Value
ActiveCell.Offset(0, -6).Range("A1").Activate
name_zip4 = ActiveCell.Value
ActiveCell.Offset(0, -6).Range("A1").Activate
date_k4 = ActiveCell.Value
ActiveCell.Offset(0, 12).Range("A1").Activate
For innerctr4 = innerstart4 To endfin
ActiveCell.Offset(1, 0).Range("A1").Activate
fifthval = ActiveCell.Value
ActiveCell.Offset(0, -6).Range("A1").Activate
name_zip5 = ActiveCell.Value
ActiveCell.Offset(0, -6).Range("A1").Activate
date_k5 = ActiveCell.Value
ActiveCell.Offset(0, 12).Range("A1").Activate
tot_val = Round(secondval + thirdval + fourthval + fifthval, 2)
If tot_val = 126.63 Then
thisRow = ActiveCell.Row
' move to memo field, put names msgbox
MsgBox " Name2: " & name_zip2 & "Name3: " & name_zip3 & "Name4: " & name_zip4 & " name_zip5 " & name_zip5
MsgBox "Date2: " & date_k2 & " Date3: " & date_k3 & " Date4: " & date_k4 & " date_k5 " & date_k5
End If
Next innerctr4
innerstart4 = innerstart3 + 2
Range("P3").Select
ActiveCell.Offset(innerctr3 + 1, 0).Range("A1").Activate
Next innerctr3
innerstart3 = innerctr2 + 2
Range("P3").Select
ActiveCell.Offset(innerctr2 + 1, 0).Range("A1").Activate
Next innerctr2
innerstart2 = innerctr1 + 2
Range("P3").Select
ActiveCell.Offset(innerctr1 + 1, 0).Range("A1").Activate
If counter = 19 Then
x = 1
End If
Next counter
End Sub
Thanks,
Gary
Bookmarks