Split numbers in 7 positions and make all possible sets adding 0 in it

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Split numbers in 7 positions and make all possible sets adding 0 in it</SPAN></SPAN>

Hello,</SPAN></SPAN>

I need to produce all possible set adding 0 in to given numbers (spiting in to 7 positions)</SPAN></SPAN>
Which finally sum should be = 7 for each of lines</SPAN></SPAN>

Here are the 16 lines from which I need to make all sets in the 7 positions adding the 0 it to it example sheet 1</SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOP
1
2
3Total NumTotal NumTotal NumTotal NumTotal NumTotal NumTotal NumTotal NumTotal NumTotal NumTotal NumTotal NumTotal NumTotal NumTotal NumTotal Num
47655444333322222
5112123123412345
61111111111
7111111
8111
91
Sheet1


Example if I use only 7 codes should create lines as shown in the example below</SPAN></SPAN>


Book1
ABCDEFGHIJ
1
2
3Total Numn1n2n3n4n5n6n7Sum
4700000077
500000707
600007007
700070007
800700007
907000007
1070000007
Sheet2


Example if I use only 6 & 1 codes should create lines as shown in the example below</SPAN></SPAN>


Book1
ABCDEFGHIJ
1
2
3Total Numn1n2n3n4n5n6n7Sum
4600000617
5100000167
600006017
700006107
800001067
900001607
1000060017
1100060107
1200061007
1300010067
1400010607
1500016007
1600600017
1700600107
1800601007
1900610007
2000100067
2100100607
2200106007
2300160007
2406000017
2506000107
2606001007
2706010007
2806100007
2901000067
3001000607
3101006007
3201060007
3301600007
3460000017
3560000107
3660001007
3760010007
3860100007
3961000007
4010000067
4110000607
4210006007
4310060007
4410600007
4516000007
Sheet3


And same way I need code could be generate for each of set is shown in the sheet1 through A:P as the used in the range A4:A9</SPAN></SPAN>

Thank you all</SPAN></SPAN>

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

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hello,</SPAN></SPAN>

Basically I need that the numbers are in the area A4:A9 out of these numbers make all possible permutations in the 7 positions which SUM is = 7 not necessary to fill the blanks with 0, may this helps </SPAN></SPAN>

Any advice please, thank you Regards, Moti</SPAN></SPAN>
 
Last edited:
Upvote 0
Hello,</SPAN></SPAN>

Mr.Excel search I find a code below under given link as per example it generate 2187 permutations with sum through 0 to 42 can it restricted so it can print out only rows just with sum = 7
https://www.mrexcel.com/forum/excel...-aca-caa-only-considered-one.html#post2349630

Please run the code with given example


Book1
ABCDEFGHIJ
1P7
2CombFALSE
3RepetTRUE
4
5Set0
61
76
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Sheet8


Code:
Option Explicit
 
' PGC Set 2007
' Calculates and writes the Combinations / Permutations with/without repetition
' vElements - Array with the set elements (1 to n)
' p - number of elements in 1 combination/permutation
' bComb - True: Combinations, False: Permutations
' bRepet - True: with repetition, False: without repetition
' vResult - Array to hold 1 permutation/combination (1 to p)
' lRow - row number. the next combination/permutation is written in lRow+1
' vResultAll - Array to hold all the permutations/combinations (1 to Total, 1 to p)
' iElement - order of the element to process in case of combination
' iIndex - position of the next element in the combination/permutation
' Sub CombPerm() deals with the input / output
' Sub CombPermNP() generates the combinations / permutations
Sub CombPerm()
Dim rRng As Range, p As Integer
Dim vElements As Variant, vResult As Variant, vResultAll As Variant, lTotal As Long
Dim lRow As Long, bComb As Boolean, bRepet As Boolean
 
' Get the inputs and clear the result range (you may adjust for other locations)
Set rRng = Range("B5", Range("B5").End(xlDown)) ' The set of numbers
p = Range("B1").Value ' How many are picked
bComb = Range("B2")
bRepet = Range("B3")
 
Columns("D").Resize(, p + 1).Clear
 
' Error
If (Not bRepet) And (rRng.Count < p) Then
    MsgBox "With no repetition the number of elements of the set must be bigger or equal to p"
    Exit Sub
End If
 
' Set up the arrays for the set elements and the result
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
With Application.WorksheetFunction
    If bComb = True Then
            lTotal = .Combin(rRng.Count + IIf(bRepet, p - 1, 0), p)
    Else
        If bRepet = False Then lTotal = .Permut(rRng.Count, p) Else lTotal = rRng.Count ^ p
    End If
End With
ReDim vResult(1 To p)
ReDim vResultAll(1 To lTotal, 1 To p)
 
' Calculate the Combinations / Permutations
Call CombPermNP(vElements, p, bComb, bRepet, vResult, lRow, vResultAll, 1, 1)
Range("D1").Resize(lTotal, p).Value = vResultAll  'you may adjust for other location
End Sub
 
Sub CombPermNP(ByVal vElements As Variant, ByVal p As Integer, ByVal bComb As Boolean, ByVal bRepet As Boolean, _
                             ByVal vResult As Variant, ByRef lRow As Long, ByRef vResultAll As Variant, ByVal iElement As Integer, ByVal iIndex As Integer)
Dim i As Integer, j As Integer, bSkip As Boolean
 
For i = IIf(bComb, iElement, 1) To UBound(vElements)
    bSkip = False
    ' in case of permutation without repetition makes sure the element is not yet used
    If (Not bComb) And Not bRepet Then
        For j = 1 To p
            If vElements(i) = vResult(j) And Not IsEmpty(vResult(j)) Then
                bSkip = True
                Exit For
            End If
        Next
    End If
 
    If Not bSkip Then
        vResult(iIndex) = vElements(i)
        If iIndex = p Then
            lRow = lRow + 1
            For j = 1 To p
                vResultAll(lRow, j) = vResult(j)
            Next j
        Else
            Call CombPermNP(vElements, p, bComb, bRepet, vResult, lRow, vResultAll, i + IIf(bComb And bRepet, 0, 1), iIndex + 1)
        End If
    End If
Next i
End Sub

Please any help would be very much appreciated
</SPAN></SPAN>


Regards,
</SPAN></SPAN>
Moti
</SPAN></SPAN>
 
Last edited:
Upvote 0
</SPAN></SPAN>Hello, I did searched in goggle in the "Mr. Excel" there are many VBA combinations, permutations but I do not find any what exactly I am looking for, combinations, permutations with target sums</SPAN></SPAN>

Need help, thank you all
</SPAN></SPAN>

Regards,
</SPAN></SPAN>
Moti
</SPAN></SPAN>
 
Last edited:
Upvote 0
Bump, May be formula than can do the job will do.....
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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