Find the optimum combination of non-overlapping groups of numbers

spavlou

New Member
Joined
Mar 14, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,
I would be absolutely grateful if someone could help me, as I am totally lost at the moment.
In cells A:F I receive 5 statistical values and in cells G:K their relevant position according to a 1 - 45 ranking system. The positions are then
sorted in ascending order in cells L:P

Cells Q:W do not belong to the spreadsheet. I just give a very simple example of what I'm trying to find.
I suppose that I need a repetitive process through a Visual Basic macro but my visual basic knowledge is limited
so, I count on your help

Note that every day I receive a new row of data, but I don't mind at all to run the macro once a day manually.

Thank you!!!

OPTIMUM_G7.xlsx
ABCDEFGHIJKLMNOPQRSTUVWX
1DAYQ-STAT VALUESUNSORTED POSITIONSSORTED POSITIONSGROUP-1GROUP-2GROUP-3SUMTOTAL CASES
2QS_1QS_2QS_3QS_4QS_5UP_1UP_2UP_3UP_4UP_5SP_1SP_2SP_3SP_4SP_5EXAMPLE ->3-1019-2638-45144<- TOTAL NUMBER OF
316.001917.00222.00237.00329.00412163319156151921331203CASES WITH AT LEAST
4215.000115.00113.00167.00395.0042873017217817213021034 POSITIONS CONTAINED
530.00161.00322.003519.00370.00394337325425323742431023IN THE 3 GROUPS
6419.00135.00212.00233.00296.00385243531195192431351203
7514.00091.00168.002417.00261.003974016536571636402013
869.000616.00186.00404.00415.00431452027245142024271203GOAL IS:
976.000316.000514.002715.00339.0036226871367813223104
10815.00021.00182.00240.00272.00396393343316313339431023TO FIND EVERY POSSIBLE
1191.00032.000645.000810.00250.00394036211412113640410022COMBINATION
12105.00131.00275.002910.003410.0044273724111010112427371102 OF 3 NON-OVERLAPPING
131111.00179.00220.002912.00310.00441113439419111341431023GROUPS OF 7
141221.00076.00090.00223.00248.00354234430164162330441113CONSECUTIVE NUMBERS
15135.00057.00160.00241.00295.0033262042392520252639420325FROM A POOL OF 1 TO 45
16144.000331.00049.00211.002210.00323031838153151830381012
17151.000513.00191.00248.00413.00444093818309183038401023CHECK FOR EVERY DAY
181620.001016.002015.00281.00320.0044478364147836413014IN CELLS L:P,
19172.00031.00054.000945.00142.00213640282342283436400011HOW MANY POSITIONS
20180.000515.00111.00208.002512.00264463918116111839441023ARE CONTAINED
211923.001210.00188.002757.003016.0042213191512513191102IN EACH ONE OF THE
22207.000715.00236.00294.004113.004319621257671921252305ABOVE GROUP OF 7
23213.000910.00133.001423.00158.00352913282162131628290000COMBINATIONS
24220.001511.00340.003514.003615.004042114165561141422024
25239.00167.00243.00303.00422.0043121529283312152829330000FIND THE
26242.00132.00148.00197.00280.0042343314174214173334420011OPTIMUM COMBINATION/S
272516.000210.00225.002721.00374.004151224327351224272103WICH GIVE
28261.00132.00166.00185.00233.0040403521242721242735400213THE HIGHEST VALUE
292715.00178.00202.00284.003430.004581833271181827331001IN CELL "V2"
302813.00047.00071.00180.00200.00458203844418203841441135
312911.00032.001311.002110.00255.0030123711142411121424370101
32302.00171.002016.003320.00394.004135377325372535372103
33319.000914.00101.00136.00195.00371483921238142123391214
343222.00081.002020.00311.00391.004133953736353637392013
35337.000213.00121.00137.00270.00312093818439182038431124
36341.00206.00347.004010.004317.00443921181344131821391113
Sheet1
Cell Formulas
RangeFormula
V2V2=COUNTIFS(U3:U735,">=4")
L3:L36L3=SMALL($G3:$K3,1)
M3:M36M3=SMALL($G3:$K3,2)
N3:N36N3=SMALL($G3:$K3,3)
O3:O36O3=SMALL($G3:$K3,4)
P3:P36P3=SMALL($G3:$K3,5)
R3:R36R3=COUNTIFS($L3:$P3,">=3",$L3:$P3,"<=10")
S3:S36S3=COUNTIFS($L3:$P3,">=19",$L3:$P3,"<=26")
T3:T36T3=COUNTIFS($L3:$P3,">=38",$L3:$P3,"<=45")
U3:U36U3=SUM(R3:T3)
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
VBA Code:
Option Explicit
Sub test()
Dim Lr&, i&, j&, sum&, count&
Lr = Cells(Rows.count, "G").End(xlUp).Row
Const pos = 4 ' define position criteria
    For i = 3 To Lr ' start from row 3 till last row
        For j = 7 To 11 ' start from column G to K
            If (Cells(i, j) >= 3 And Cells(i, j) <= 10) Or (Cells(i, j) >= 19 And Cells(i, j) <= 26) Or (Cells(i, j) >= 38 And Cells(i, j) <= 45) Then _
            count = count + 1
        Next
        If count >= pos Then sum = sum + 1
        count = 0
    Next
MsgBox sum ' to read sum
'Or to paste sum into cell V2
'Range("V2").Value = sum
End Sub
 
Upvote 0
Solution
VBA Code:
Option Explicit
Sub test()
Dim Lr&, i&, j&, sum&, count&
Lr = Cells(Rows.count, "G").End(xlUp).Row
Const pos = 4 ' define position criteria
    For i = 3 To Lr ' start from row 3 till last row
        For j = 7 To 11 ' start from column G to K
            If (Cells(i, j) >= 3 And Cells(i, j) <= 10) Or (Cells(i, j) >= 19 And Cells(i, j) <= 26) Or (Cells(i, j) >= 38 And Cells(i, j) <= 45) Then _
            count = count + 1
        Next
        If count >= pos Then sum = sum + 1
        count = 0
    Next
MsgBox sum ' to read sum
'Or to paste sum into cell V2
'Range("V2").Value = sum
End Sub

I apologize because my English are not very good and I was not very clear about my goal.
However, your code was perfect because it gave me a good idea of how to solve my problem.
Thanks a lot for your time!!!
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top