Complicated VBA with 2 Conditions, 1 = Row target Sum, 2= Single number in the row

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,362
Office Version
  1. 2010
Complicated VBA with 2 Conditions, 1 = Row target Sum, 2= Single number in the row</SPAN></SPAN>

Hello,</SPAN></SPAN>

I am looking some specific solution I will try my best to explain here, if it is not clear. Please ask any question I will try to make it clearer</SPAN></SPAN>


There is total 9 Numbers, which are as follow---> 1, 2, 3, 4, 5, 6, 7, 8, and 9, Column A</SPAN></SPAN>

Out of 9 numbers any quantity of numbers can be used in the row to make combinations of 7 in the each rows in the columns D:J</SPAN></SPAN>

I want also a VBA that can apply the following conditions...</SPAN></SPAN>

Condition 1- as per given example row target sum could be selected as desired in the column K (in this example selected sum = 33)</SPAN></SPAN>

Condition 2- as per example given single number 0 to 7 can be used desired in the column L </SPAN></SPAN>
Explanations of condition-2, for examples...</SPAN></SPAN>
L4=0 in the row D4:J4 used 1=3 times, 7=2 times, 8=2 times (Used Single number 0 time)</SPAN></SPAN>

L5=1 in the row D5:J5 used 1=2 times, 4=2 times, 9=2times (5 is used as a single number so far 1 number is single used) same condition is applied to row L5:L11</SPAN></SPAN>

L12=2 in the row D12:J12 used 1=2 times, 8=3 times (3 & 4 is used as a single number so far 2 numbers are single used) same condition is applied to row L12:L18</SPAN></SPAN>

L19=3 in the row D12:J12 used 1=2 times, 9=2 times (3, 6 & 9 is used as a single number so far 3 numbers are single used) same condition is applied to row L19:L37</SPAN></SPAN>

And same you will see for 4, 5 & 7 </SPAN></SPAN>

Example </SPAN></SPAN>


Book1
ABCDEFGHIJKL
1
2Condition 1Condition 2
3Numbersn1n2n3n4n5n6n7Row SumUse Single Numbers In The Row
411787118330
521144995331
631771791331
741814847331
852281668331
962323599331
1074184871331
1189351915331
1291384188332
133336882332
144634943332
155715555332
167228248332
178331378332
188414646332
191319649333
203391656333
213569352333
223648318333
233619653333
244196391333
254477218333
264536555333
275166285333
285435736333
296416772333
306514818333
316921186333
327347381333
337452456333
348195541333
358841633333
369271914333
379391164333
381558653334
391575258334
403968313334
414333569334
426537552334
437952262334
448697111334
451195863335
461718394335
471875372335
482291865335
492651883335
502855319335
512863527335
522942376335
533129738335
543157944335
553457941335
563879312335
574259814335
584585317335
594662195335
605341785335
615438931335
625628237335
635683254335
645954613335
656453591335
666471951335
676474831335
686537147335
696943425335
707282365335
717842219335
728256138335
738349171335
748517345335
759264651335
762547861337
772863149337
784571628337
795627841337
806275814337
816541728337
828467152337
839481632337
84
85
Sheet6


Thank you all</SPAN></SPAN>

Excel 2000</SPAN></SPAN>
Regards,</SPAN>
Moti</SPAN>
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this:-
NB:- This code takes approx. 10secs to run!!!
Assuming I understand your requirements then:-
This code is based on conditions in columns "K & L".( These values are required )
The code runs to row 83 !!!
Code:
[COLOR="Navy"]Sub[/COLOR] MG16Nov04
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Tot [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] oMin [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] cols [COLOR="Navy"]As[/COLOR] Variant
cols = Array(6, 43, 38, 39, 5, 3, 8, 40, 10)
Application.ScreenUpdating = False

[COLOR="Navy"]For[/COLOR] Rw = 4 To 83
Tot = 0
Randomize
[COLOR="Navy"]Do[/COLOR] Until Tot = Cells(Rw, 11) And oMin = Cells(Rw, 12)
    Tot = 0: oMin = 0
    [COLOR="Navy"]For[/COLOR] n = 1 To 7
        Num = Int(Rnd * 9) + 1
        Tot = Tot + Num
        Cells(Rw, n + 3) = Num
        Cells(Rw, n + 3).Font.ColorIndex = 1
        Cells(Rw, n + 3).Interior.ColorIndex = cols(Num - 1)
        [COLOR="Navy"]If[/COLOR] Num = 5 Or Num = 6 Or Num = 9 [COLOR="Navy"]Then[/COLOR]
            Cells(Rw, n + 3).Font.ColorIndex = 2
        [COLOR="Navy"]Else[/COLOR]
            Cells(Rw, n + 3).Font.ColorIndex = 1
        [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]For[/COLOR] Ac = 4 To 10
    oMin = oMin + IIf(Application.CountIf(Cells(Rw, 4).Resize(, 10), Cells(Rw, Ac)) = 1, 1, 0)
[COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]Loop[/COLOR]
[COLOR="Navy"]Next[/COLOR] Rw
Application.ScreenUpdating = True
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Try this:-
NB:- This code takes approx. 10secs to run!!!
Assuming I understand your requirements then:-
This code is based on conditions in columns "K & L".( These values are required )
The code runs to row 83 !!!
Regards Mick
MickG, I want to thank you for looking a my request that is really complicated, I done various test it seems condition-1 in the column K works perfect but condition-2 in the column L fails every time I run the code it gives different results both conditions values are there in the column K & L</SPAN></SPAN>

To check I have created in column O:Y to test the output results
</SPAN></SPAN>
Formula in O4 =COUNTIF($D4:$J4,O$3) across & down to W83
</SPAN></SPAN>
Formula in X4 =SUM(D4:J4) down to X83
</SPAN></SPAN>
Formula in Y4 =COUNTIF(O4:W4,1)
</SPAN></SPAN>

Here is the result.... Use Single Numbers In The Row does not match
</SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOPQRSTUVWXY
1
2Condition 1Condition 2Condition 1Condition 2
3Numbersn1n2n3n4n5n6n7Row SumUse Single Numbers In The Row123456789Row SumUse Single Numbers In The Row
415577225330020030200330
526446229331020202001331
636836136331102003010332
748137383331103000120332
851919373331202000102331
961417479331200200201331
1077266723331021002200331
1184144992331110300002332
1292949117332210100102333
135835732332012020110333
144477362332011201200333
152187186332210001120333
168542338332012110020333
173473349332003200101332
181744737332101200300332
191628916333210002011333
206556218333110022010333
217627245333020111200333
225563725333011031100334
238126961333210002011333
244463574333001311100334
256217575333110021200333
261154787333200110210333
273665193333102012001333
283258375333012020110333
295253945333011130001334
303646716333101103100334
312655735333011031100334
326227691333120002101333
332542659333020121001333
345165187333200021110333
352192964333120101002333
369611772333210001201333
372871168333210001120333
382932278334031000111334
394653474334001311100334
401174389334201100111335
419322584334021110011335
426586134334101112010335
436349515334101121001335
444155693334101121001335
458953512335111020011335
469173823335112000111335
479714336335102101101335
484762149335110201101335
492487831335111100120335
507139283335112000111335
513748182335111100120335
521983525335111020011335
532974641335110201101335
543475518335101120110335
557713924335111100201335
569734721335111100201335
574195527335110120101335
582318766335111002110335
594227936335021101101335
607496124335110201101335
613648471335101201110335
627413972335111100201335
635152974335110120101335
649326472335021101101335
659741138335201100111335
663526485335011121010335
671393764335102101101335
684179624335110201101335
694327197335111100201335
707963341335102101101335
712463927335021101101335
724851537335101120110335
737971423335111100201335
745981253335111020011335
757348218335111100120335
762463189337111101011337
771293468337111101011337
786423981337111101011337
799816324337111101011337
808619432337111101011337
812138469337111101011337
822618934337111101011337
831293864337111101011337
84
85
Sheet7


Here is another simplified example also does not match may this help
</SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOPQRSTUVWXY
1
2Condition 1Condition 2Condition 1Condition 2
3Numbersn1n2n3n4n5n6n7Row SumUse Single Numbers In The Row123456789Row SumUse Single Numbers In The Row
412394414273111300001274
524542912273120210001273
636551433273102121000273
746451335273102121000273
854738131273202100110273
962341944273111300001274
1071319571273301010101274
1182743317273112100200273
1295791131273301010101274
133454137273102210100273
143711519273301010101274
154672332273022101100273
163397221273122000101273
176534234273012211000273
182218293273131000011274
193217932273122000101273
202238462273031101010274
211256751273210021100273
221419426273210201001273
239228132273131000011274
244152159273210120001273
259215145273210120001273
263933531273104010001273
271215297273220010101273
281921257273220010101273
294291551273210120001273
302421594273120210001273
312732931273122000101273
321318653273202011010273
338444214273110400010273
342514258273120120010273
351181376273301001110274
361327923273122000101273
379335133273104010001273
382477115273210110200273
391168191273400001011273
402824641273120201010273
411194624273210201001273
422482326273031101010274
434327326273022101100273
443536451273102121000273
453326544273012211000273
463262743273022101100273
474552191273210120001273
486534234273012211000273
493111876273301001110274
507822161273220001110273
516222852273040011010273
524521249273120210001273
535155632273111031000274
542642283273031101010274
551244565273110221000273
564169412273210201001273
572452392273031110001274
581282491273220100011273
592617218273220001110273
601155627273210021100273
613161718273301001110274
622219832273131000011274
631469241273210201001273
641924821273220100011273
653615552273111031000274
664861422273120201010273
676131349273202101001273
688622171273220001110273
692453346273012211000273
702911824273220100011273
714651344273101311000274
721572714273210110200273
736223284273031101010274
747342137273112100200273
752734713273112100200273
761814229273220100011273
774345731273102210100273
783264543273012211000273
793852513273112020010273
802171682273220001110273
812841192273220100011273
821319715273301010101274
833627333273014001100273
84
85
Sheet8


Please could you take a look?
</SPAN></SPAN>



Regards,
</SPAN>
Moti
</SPAN>
 
Upvote 0
Try changing the "10" to a "7" (Counting error), in line below:-
Code:
oMin = oMin + IIf(Application.CountIf(Cells(Rw, 4).Resize(,[COLOR="#FF0000"][B][SIZE=4] 7), [/SIZE][/B][/COLOR]Cells(Rw, Ac)) = 1, 1, 0)
 
Upvote 0
Try changing the "10" to a "7" (Counting error), in line below:-
Code:
oMin = oMin + IIf(Application.CountIf(Cells(Rw, 4).Resize(,[COLOR=#ff0000][B][SIZE=4] 7), [/SIZE][/B][/COLOR]Cells(Rw, Ac)) = 1, 1, 0)
Amazing Mick, I am sure this were a difficult and challenging task to code it as per request. Hats off to you! As you said changing the "10" to a "7" macro worked great it return all results as per both conditions in columns "K & L" I am very happy to see the results </SPAN></SPAN>

I appreciate your time and effort to help me out.
</SPAN></SPAN>

Have a great weekend and good luck to you.
</SPAN></SPAN>

Kind regards,
</SPAN></SPAN>
Moti
</SPAN></SPAN>
 
Upvote 0

Forum statistics

Threads
1,215,333
Messages
6,124,317
Members
449,153
Latest member
JazzSingerNL

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