Function MaxAddress(The_Range)
MaxNum = Application.Max(The_Range)
For Each cell In The_Range
If cell = MaxNum Then
MaxAddress = cell.Address
Exit For
End If
Next cell
End Function
Sub Test()
'On Error GoTo ErrHndlr
'Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
G1LRow = WorksheetFunction.CountA(Range("A:A"))
G2LRow = WorksheetFunction.CountA(Range("D:D"))
G3LRow = WorksheetFunction.CountA(Range("G:G"))
G4LRow = WorksheetFunction.CountA(Range("J:J"))
MaxGRow = WorksheetFunction.Max(G1LRow, G2LRow, G3LRow, G4LRow)
Clr1LRow = WorksheetFunction.CountA(Range("N:N"))
Clr2LRow = WorksheetFunction.CountA(Range("O:O"))
Clr3LRow = WorksheetFunction.CountA(Range("P:P"))
Clr4LRow = WorksheetFunction.CountA(Range("Q:Q"))
MaxClrRow = WorksheetFunction.Max(Clr1LRow, Clr2LRow, Clr3LRow, Clr4LRow)
Range("N4:Q" & MaxClrRow).ClearContents
Range("M6:M" & MaxClrRow).ClearContents
'Sort Group 1
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A3:A" & G1LRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B3:B" & G1LRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A2:B" & G1LRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Sort Group 2
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("D3:D" & G2LRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("E3:E" & G2LRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("D2:E" & G2LRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Sort Group 3
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("G3:G" & G3LRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("H3:H" & G3LRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("G2:H" & G3LRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Sort Group 4
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("J3:J" & G4LRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("K3:K" & G4LRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("J2:K" & G4LRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
G1ValueMax = Range("B" & G1LRow).Value
G2ValueMax = Range("E" & G2LRow).Value
G3ValueMax = Range("H" & G3LRow).Value
G4ValueMax = Range("K" & G4LRow).Value
G1PriceMax = Range("A" & G1LRow).Value
G2PriceMax = Range("D" & G2LRow).Value
G3PriceMax = Range("G" & G3LRow).Value
G4PriceMax = Range("J" & G4LRow).Value
If Range("N3").Value = 0 Then
Range("N4").ClearContents
Else
Range("N4").Value = G1ValueMax
End If
If Range("O3").Value = 0 Then
Range("O4").ClearContents
Else
Range("O4").Value = G2ValueMax
End If
If Range("P3").Value = 0 Then
Range("P4").ClearContents
Else
Range("P4").Value = G3ValueMax
End If
If Range("Q3").Value = 0 Then
Range("Q4").ClearContents
Else
Range("Q4").Value = G4ValueMax
End If
If Range("N3").Value = 0 Then
Range("N5").ClearContents
Else
Range("N5").Value = G1PriceMax
End If
If Range("O3").Value = 0 Then
Range("O5").ClearContents
Else
Range("O5").Value = G2PriceMax
End If
If Range("P3").Value = 0 Then
Range("P5").ClearContents
Else
Range("P5").Value = G3PriceMax
End If
If Range("Q3").Value = 0 Then
Range("Q5").ClearContents
Else
Range("Q5").Value = G4PriceMax
End If
For1:
If Range("N3").Value <= 1 Then
GoTo For2
End If
For i = 2 To Range("N3").Value
Range("N" & (i * 2) + 2).Value = Range("B" & G1LRow - i + 1).Value
Range("M" & (i * 2) + 2).Value = "Next Value"
Range("N" & (i * 2) + 3).Value = Range("A" & G1LRow - i + 1).Value
Range("M" & (i * 2) + 3).Value = "Next Price"
Next i
For2:
If Range("O3").Value <= 1 Then
GoTo For3
End If
For i = 2 To Range("O3").Value
Range("O" & (i * 2) + 2).Value = Range("E" & G2LRow - i + 1).Value
Range("M" & (i * 2) + 2).Value = "Next Value"
Range("O" & (i * 2) + 3).Value = Range("D" & G2LRow - i + 1).Value
Range("M" & (i * 2) + 3).Value = "Next Price"
Next i
For3:
If Range("P3").Value <= 1 Then
GoTo For4
End If
For i = 2 To Range("P3").Value
Range("P" & (i * 2) + 2).Value = Range("H" & G3LRow - i + 1).Value
Range("M" & (i * 2) + 2).Value = "Next Value"
Range("P" & (i * 2) + 3).Value = Range("G" & G3LRow - i + 1).Value
Range("M" & (i * 2) + 3).Value = "Next Price"
Next i
For4:
If Range("Q3").Value <= 1 Then
GoTo ForEnd
End If
For i = 2 To Range("Q3").Value
Range("Q" & (i * 2) + 2).Value = Range("K" & G4LRow - i + 1).Value
Range("M" & (i * 2) + 2).Value = "Next Value"
Range("Q" & (i * 2) + 3).Value = Range("J" & G4LRow - i + 1).Value
Range("M" & (i * 2) + 3).Value = "Next Price"
Next i
ForEnd:
For t = 3 To MaxGRow
Calculate
If Range("R5").Value < Range("M3").Value Then
'Everything's good. End this.
GoTo ErrHndlr
Else
'back up 1 on the biggest $amount in the bottom of the ranges
Rg1 = WorksheetFunction.CountA(Range("N6:N" & MaxGRow * 2))
Rg2 = WorksheetFunction.CountA(Range("O6:O" & MaxGRow * 2))
Rg3 = WorksheetFunction.CountA(Range("P6:P" & MaxGRow * 2))
Rg4 = WorksheetFunction.CountA(Range("Q6:Q" & MaxGRow * 2))
MxRg = WorksheetFunction.Max(Rg1, Rg2, Rg3, Rg4)
KillTheBigGuns:
If MxRg = 0 Then
Calculate
If Range("R5").Value < Range("M3").Value Then
'Everything's good. End this.
GoTo ErrHndlr
End If
ChngAdrs2 = MaxAddress(Range("N5:Q5"))
ChngAdrs1 = Cells(Range(ChngAdrs2).Row - 1, Range(ChngAdrs2).Column).Address
MaxCol = ((Range(ChngAdrs2).Column - 13) * 3) - 1
ChkAdrs = Cells(1, MaxCol).Address & ":" & Cells(MaxGRow, MaxCol).Address
NextPos = Range(ChkAdrs).Rows.Count + 1
If NextPos - 2 - (t - 3) < 3 Then
MsgBox "You will need to change either your number of selections or your dollar amounts. The total of the smallest values available at these quantities represent a total price over your limit of $" & Range("M3").Value
GoTo ErrHndlr
End If
BackupOne1 = Range(Cells(NextPos - 2 - (t - 3), MaxCol).Address).Value
BackupOne2 = Range(Cells(NextPos - 2 - (t - 3), MaxCol - 1).Address).Value
MsgBox BackupOne1
MsgBox BackupOne2
If BackupOne1 = 0 Or BackupOne2 = 0 Then
MsgBox "You will need to change either your number of selections or your dollar amounts. The total of the smallest values available at these quantities represent a total price over your limit of $" & Range("M3").Value
GoTo ErrHndlr
End If
Range(ChngAdrs2).Value = BackupOne2
Range(ChngAdrs1).Value = BackupOne1
If KillBigGuns = "yes" Then
GoTo KillTheBigGuns
Else
GoTo Nxtt
End If
Else
Rg1 = WorksheetFunction.CountA(Range("N6:N" & MaxGRow * 2))
Rg2 = WorksheetFunction.CountA(Range("O6:O" & MaxGRow * 2))
Rg3 = WorksheetFunction.CountA(Range("P6:P" & MaxGRow * 2))
Rg4 = WorksheetFunction.CountA(Range("Q6:Q" & MaxGRow * 2))
MxRg = WorksheetFunction.Max(Rg1, Rg2, Rg3, Rg4)
ChngAdrs2 = MaxAddress(Range("N6:Q" & MxRg + 6))
ChngAdrs1 = Cells(Range(ChngAdrs2).Row - 1, Range(ChngAdrs2).Column).Address
MaxPos = ((Range(MaxAddress(Range("N6:Q" & MxRg + 6))).Row - 3) / 2) - 1
MaxCol = ((Range(MaxAddress(Range("N6:Q" & MxRg + 6))).Column - 13) * 3) - 1
ChkAdrs = Cells(1, MaxCol).Address & ":" & Cells(MaxGRow, MaxCol).Address
If Range(ChkAdrs).Rows.Count - MaxPos - 2 - (t - 3) < 3 Then
MxRg = 0
KillBigGuns = "yes"
GoTo KillTheBigGuns
End If
BackupOne1 = Range(Cells(Range(ChkAdrs).Rows.Count - MaxPos - 2 - (t - 3), MaxCol).Address).Value
BackupOne2 = Range(Cells(Range(ChkAdrs).Rows.Count - MaxPos - 2 - (t - 3), MaxCol - 1).Address).Value
Range(ChngAdrs2).Value = BackupOne2
Range(ChngAdrs1).Value = BackupOne1
End If
End If
Nxtt:
Next t
ErrHndlr:
Range("M5") = "Max Price"
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
End Sub
This looks at the max $ as input value in cell M3, and a formula that calculates the sum of the total price in cell R5. I've attached the version of your workbook containing the formula and the code. Try it out. I hope you can make it work.
Bookmarks