From the 8 numbers, generate permutations with set of 9 numbers, with target sum of 7

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,362
Office Version
  1. 2010
Hello,

Please I need help with permutations.

Data-1 I got 8 numbers in the column "A" cell A6:A13 (0,1,2,3,4,5,6 and 7)
Data-2 my target permutation sum of each line, which is 7, is in the cell B3

I want using above data create all possible permutations with set of 9 numbers in the columns C:K and sum target of each line must be 7 as shown in the column M

For this example result, sample image is attached.

*ABCDEFGHIJKLMNO
1
2Find
3Sum7
4
5Numbersn1n2n3n4n5n6n7n8n9Sum
601011110117
710111111107
820000220127
930001120217
1040001211207
1150001301027
1260002130107
1370002311007
140003101027
150010111127
161031101007
171040001017
181040011007
191100102117
201113010007
211113100007
221113100007
231120010207
241130101007
251131001007
261203100007
271204000007
281204000007
291411000007
302000000327
312220000017
322220100007
332300000117
343011101007
353011110007
365101000007
375101000007
385110000007
395110000007
405110000007
416000000107
426000001007
436001000007
446001000007
456100000007
467000000007
47
48

Thank you all.

I am using Excel 2000

Regards,
Moti
 

Attachments

  • 8 Number 9 Permutation.png
    8 Number 9 Permutation.png
    37.6 KB · Views: 12

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.
Hello,

Finally I got a code working as per my need with my version results in 0:00:01 seconds

Thank you all for assisting here for my query, I am living code here may some one require.

VBA Code:
Sub SplitInTo9Cells_8Num_0To7_BySumA6()

    
      Range("C6:M6441").ClearContents
      Range("C6").Select
'-----------------------------Timer----------------------------------------------------------
Dim dtStartTime As Date
    dtStartTime = Now()
'-----------------------------Timer----------------------------------------------------------

Dim nextRow As Long
Dim n1 As Long, n2 As Long, n3 As Long, n4 As Long, n5 As Long, n6 As Long, n7 As Long, n8 As Long, n9 As Long

'-------------------------------------------------------------------------------------------
    'Use ScreenUpdating property to turn screen updating off to speed up your macro code:
    'Prevents screen refreshing.
    Application.ScreenUpdating = False
'-------------------------------------------------------------------------------------------

nextRow = 5

For n1 = 0 To 7
 For n2 = 0 To 7 - n1
  For n3 = 0 To 7 - n1 - n2
   For n4 = 0 To 7 - n1 - n2 - n3
    For n5 = 0 To 7 - n1 - n2 - n3 - n4
     For n6 = 0 To 7 - n1 - n2 - n3 - n4 - n5
      For n7 = 0 To 7 - n1 - n2 - n3 - n4 - n5 - n6
       For n8 = 0 To 7 - n1 - n2 - n3 - n4 - n5 - n6 - n7
        For n9 = 0 To 7 - n1 - n2 - n3 - n4 - n5 - n6 - n7 - n8
            
          
          RowSum = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9
          If RowSum = Range("B3").Value Then
          

             nextRow = nextRow + 1
            
             Cells(nextRow, 3).Value = n1
             Cells(nextRow, 4).Value = n2
             Cells(nextRow, 5).Value = n3
             Cells(nextRow, 6).Value = n4
             Cells(nextRow, 7).Value = n5
             Cells(nextRow, 8).Value = n6
             Cells(nextRow, 9).Value = n7
             Cells(nextRow, 10).Value = n8
             Cells(nextRow, 11).Value = n9

         End If
        
            
        Next
       Next
      Next
     Next
    Next
   Next
  Next
 Next
Next


'------------------------------------------------------------------------------------------------------------------------------
   'You won't be able to see what the macro is doing, but it will run faster. When your macro ends don't forget to set the
   'Enables screen refreshing.
   Application.ScreenUpdating = True
'------------------------------------------------------------------------------------------------------------------------------
 
  Dim lngLastRow As Long
  lngLastRow = Cells(Rows.Count, "C").End(xlUp).Row
 
  Range("M6:M" & lngLastRow).Formula = "=SUM(C7:K7)"
  Range("M7:M" & lngLastRow) = Range("M7:M" & lngLastRow).Value 'Convert Values
 
 
'-----------------------------Timer----------------------------------------------------------
MsgBox "Macro ran successfully in " & _
            FormatDateTime(Now() - dtStartTime, 3), vbInformation
'-----------------------------Timer----------------------------------------------------------


End Sub


Have a nice weekend

Kind Regards,
Moti :)
 
Upvote 0
What happens with this code if you change the numbers values in A6:A13 ? …​
'Your code' needs more than 3s on my tests old laptop computer, working with arrays should need less than 0.3s …​
 
Upvote 0
What happens with this code if you change the numbers values in A6:A13 ? …​
Marc L, totally agree with you I am not able to code this option so far I live it unsolved may some can help to make a code which can read the values are in cell A6:A13...that would be really great.

Thank you for your observations, have a good start of the week

Kind Regards,
Moti
 
Upvote 0
Without any answer to my posts #9 & #11 question - but according to your post #12 code I presume it's yes - this is a one shot demonstration
(needs 0.2s on my side, ready since post #5 - in fact since last century ! - but meaning I won't modify its code whatever the reason …)​
to paste to the top of a module (the better place is the worksheet module) :​
VBA Code:
Dim P%(), R&, S%(), V()

Sub PSum(T%, Optional ByVal C% = 1)
    Dim N&, Z%
    For N = 1 To UBound(V)
        Z = T - V(N):  If Z < 0 Then Exit Sub
        P(C) = V(N)
        If C < UBound(P) Then PSum Z, C + 1 Else If Z = 0 Then R = R + 1: For C = 1 To C: S(R, C) = P(C): Next: Exit Sub
    Next
End Sub

Sub Demo1()
        T! = Timer
    With [C5].CurrentRegion.Rows
        If .Columns.Count = 1 Then Beep: Exit Sub
        If .Count > 1 Then .Item("2:" & .Count).ClearContents
    With Range("A6", [A5].End(xlDown))
        V = Application.Small(.Value2, Evaluate("COLUMN(" & [A1].Resize(, .Rows.Count).Address & ")"))
    End With
        If [B3].Value2 > V(UBound(V)) * .Columns.Count Then Beep: Erase V: Exit Sub
        ReDim P(1 To .Columns.Count), S(1 To Rows.Count - 5, 1 To .Columns.Count)
        R = 0
        PSum [B3].Value2
        If R Then .Item(2).Resize(R).Value2 = S
    End With
        Debug.Print Format(Timer - T, "0.000s")
        Erase P, S, V
End Sub
 
Upvote 0
Solution
Without any answer to my posts #9 & #11 question - but according to your post #12 code I presume it's yes - this is a one shot demonstration
(needs 0.2s on my side, ready since post #5 - in fact since last century ! - but meaning I won't modify its code whatever the reason …)​
to paste to the top of a module (the better place is the worksheet module) :​
VBA Code:
Dim P%(), R&, S%(), V()

Sub PSum(T%, Optional ByVal C% = 1)
    Dim N&, Z%
    For N = 1 To UBound(V)
        Z = T - V(N):  If Z < 0 Then Exit Sub
        P(C) = V(N)
        If C < UBound(P) Then PSum Z, C + 1 Else If Z = 0 Then R = R + 1: For C = 1 To C: S(R, C) = P(C): Next: Exit Sub
    Next
End Sub

Sub Demo1()
        T! = Timer
    With [C5].CurrentRegion.Rows
        If .Columns.Count = 1 Then Beep: Exit Sub
        If .Count > 1 Then .Item("2:" & .Count).ClearContents
    With Range("A6", [A5].End(xlDown))
        V = Application.Small(.Value2, Evaluate("COLUMN(" & [A1].Resize(, .Rows.Count).Address & ")"))
    End With
        If [B3].Value2 > V(UBound(V)) * .Columns.Count Then Beep: Erase V: Exit Sub
        ReDim P(1 To .Columns.Count), S(1 To Rows.Count - 5, 1 To .Columns.Count)
        R = 0
        PSum [B3].Value2
        If R Then .Item(2).Resize(R).Value2 = S
    End With
        Debug.Print Format(Timer - T, "0.000s")
        Erase P, S, V
End Sub
Marc L, I am sorry for replying you late because I did not had access to my desktop computer, I actually tried your code just now and it's working pretty well, actually better than mine as you suggested. Got results in 0:00:00 seconds, also it is reading the values are in cell A6:A13...that is really great option added by you.

Thanks for you help once again. Have a good start of the week ahead

Kind Regards
Moti :)
 
Upvote 0

Forum statistics

Threads
1,215,092
Messages
6,123,064
Members
449,090
Latest member
fragment

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