vba convert the formula combin into code

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
986
Office Version
  1. 2010
Platform
  1. Windows
Hi.

Combin(i$5,6)-IF(I$5-5B6>0,COMBIN(I$5-B6,6),0)-IF(I$5-4-C6>0,COMBIN(I$5-C6,5),0)-IF(I$5-3-d6>0,COMBIN(I$5-D6,4)

I would like to use a code for this formula, rather than scroll down or up all the time.
Any help for this Please.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
It's not clear what you are doing, and why you need to scroll up or down?

Are you copying this formula down a column? What values do you have in B6:D6? What about B7:D7, B8:D8 etc?

Can you show us your layout via a screenshot, preferably using the Forum's XL2BB add-in.
 
Upvote 0
StephenCrump thank you Sir for your reply.
this is what I have:
<tbody>
ABCDEFGHIJK
5/8/2019​
1​
11​
12​
24​
31​
33​
1,643,117.00
5/4/2019​
1​
4​
14​
33​
36​
43​
608,539.00
5/1/2019​
18​
27​
28​
30​
35​
43​
21,253,956.00
4/27/2019​
1​
17​
27​
36​
42​
51​
53​
2,206,281.00
4/24/2019​
2​
4​
21​
27​
32​
45​
3,002,358.00
4/20/2019​
4​
15​
19​
23​
31​
53​
8,421,055.00
4/17/2019​
2​
25​
31​
45​
52​
53​
4,842,369.00
4/13/2019​
5​
18​
28​
37​
42​
49​
10,348,067.00
4/10/2019​
18​
22​
34​
39​
48​
53​
21,160,159.00
4/6/2019​
4​
8​
36​
37​
40​
50​
7,748,884.00
4/3/2019​
9​
18​
22​
37​
44​
46​
15,541,728.00
3/30/2019​
10​
13​
23​
49​
51​
53​
16,175,608.00
3/27/2019​
13​
17​
19​
23​
39​
50​
18,691,578.00
</tbody>
When I update this array always is in top, reason why I say scroll up or if I copy the array somewhere else scroll down, and this array is B2:G LastRow, is a "dynamic array". at this moment this array is ("B2:G2752").
Thank you Mr. StephenCrump.
 
Upvote 0
Sorry, I still don't understand the problem. Can you please describe in more detail what you are trying to do?

I am guessing (based on our discussions in other threads) that you are writing these formulae using VBA, and that you want your code to put similar formulae into other cells? If so, it would help if you let us know what these other formulae are, and where you want to put them.

And if you are already using code, can you please post it, thanks.
 
Upvote 0
Really sorry I am not clear.
what I am trying to do is to get the index number of any combination of the 6 numbers taking out of 53, so is about 23 millions combinations, I hava a list of 2.500 more or less and growing I would like every time I update the list, just play the code I get the index number of the next combination. And on I5 I have the number 53, I would like to move to B1.
In the example column K have the index numbers, but this is not about the formula is working, is that a code is better for me. sorry.
And this formula is not connected with the other threads before, so I have one formula, and no codes at all; where I want to putting, what about column H, would be fine.
thank you Mr. Stephen Crump.
 
Upvote 0
By index number, I think you mean something like this?

GetNthCombination.xlsm
ABCDEFG
1IndexCombination---->
21123456
32123457
43123458
54123459
61001234712
71,00012343448
8100,000128354042
91,000,0001617193741
1022,957,480484950515253
Sheet1
Cell Formulas
RangeFormula
B2:G10B2=GetNthCombination(53,6,A2)
A10A10=COMBIN(53,6)
Dynamic array formulas.


I have the following code to go from, for example, Index 1,000,000 ----> Combination 1, 6, 17, 19, 37, 41.

VBA Code:
Function GetNthCombination(N As Long, r As Long, ByVal cCombinationNo As Currency) As Long()

    Dim lCombination() As Long, i As Long, j As Long
    Dim cTemp As Currency
    ReDim lCombination(1 To r)
    
    j = 0
    For i = 1 To r
        Do
            j = j + 1
            cTemp = Round(Application.Combin(N - j, r - i), 0)
            If cCombinationNo <= cTemp Then
                lCombination(i) = j
                Exit Do
            End If
            cCombinationNo = cCombinationNo - cTemp
        Loop
    Next i
    
    GetNthCombination = lCombination

End Function

I think you want to go in the opposite direction, e.g. you have Combination 1, 6, 17, 19, 37, 41, and you want the formula to return the index number 1,000,000?
 
Upvote 0
Thank you Mr. Stephen Crump.
You are right, I have the combinations, and would like to see the index numbers for any combination I will upload to the sheet.
and also Thank you for the function, is really handy.
Thank you for your time Mr. Stephen Crump.
 
Upvote 0
Here's one way could code it:

GetNthCombination.xlsm
ABCDEFGHI
1Combination---->Index
21234561
31234572
41234583
51234594
61234712100
7123434481,000
8128354042100,000
916171937411,000,000
1048495051525322,957,480
Sheet2
Cell Formulas
RangeFormula
I2:I10I2=GetIndexNumber(53,6,B2:G2)


VBA Code:
Function GetIndexNumber(N As Long, r As Long, Vector As Variant) As LongLong

    Dim Combination As Variant
    Dim i As Long
    Dim Total As LongLong
    
    Combination = Vector
    If TypeOf Vector Is Range Then
        Combination = Application.Transpose(Vector)
        If Vector.Columns.Count > 1 Then Combination = Application.Transpose(Combination)
    End If
    Total = Application.Combin(N, r)
    
    On Error Resume Next
    For i = LBound(Combination) To UBound(Combination)
        Total = Total - Application.Combin(N - Combination(i), r + LBound(Combination) - i)
    Next i
    On Error GoTo 0
    
    GetIndexNumber = Total

End Function
 
Upvote 0
Hi, Sir.
Thank you for your reply.
I use the formula and give me #NAME?, and the function, I insert a module in my IDE and not results,
Please excuse my coding level, if it is possible a regular sub where I can see the destination location, like range("O:O").
 
Upvote 0
See workbook attached: Box

GetNthCombination.xlsm
ABCDEFGHI
1MyN53
2MyR6
3
4IndexCombination---->Index
511234561
621234572
731234583
841234594
91001234712100
101,000123434481,000
11100,000128354042100,000
121,000,00016171937411,000,000
1322,957,48048495051525322,957,480
1
Cell Formulas
RangeFormula
B5:G13B5=GetNthCombination(MyN,MyR,A5)
A13A13=COMBIN(MyN,MyR)
I5:I13I5=GetIndexNumber(MyN,MyR,B5:G5)
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
Named Ranges
NameRefers ToCells
MyN='1'!$B$1A13:G13, B5:G12, I5:I13
MyR='1'!$B$2A13:G13, B5:G12, I5:I13


The formulae don't display well above:
Select B5:G5 and array-enter the formula: {=GetNthCombination(MyN,MyR,A5)}
Copy down the column.

Or for those with Excel 365 dynamic functions, simply enter B5: =GetNthCombination(MyN,MyR,A5)

VBA Code:
Function GetNthCombination(N As Long, r As Long, ByVal CombinationNo As LongLong) As Long()

    Dim Combination() As Long, i As Long, j As Long
    Dim Temp As LongLong
    ReDim Combination(1 To r)
   
    j = 0
    For i = 1 To r
        Do
            j = j + 1
            Temp = Round(Application.Combin(N - j, r - i), 0)
            If CombinationNo <= Temp Then
                Combination(i) = j
                Exit Do
            End If
            CombinationNo = CombinationNo - Temp
        Loop
    Next i
   
    GetNthCombination = Combination

End Function
Function GetIndexNumber(N As Long, r As Long, Vector As Variant) As LongLong

    Dim Combination As Variant
    Dim i As Long
    Dim Total As LongLong
   
    Combination = Vector
    If TypeOf Vector Is Range Then
        Combination = Application.Transpose(Vector)
        If Vector.Columns.Count > 1 Then Combination = Application.Transpose(Combination)
    End If
    Total = Application.Combin(N, r)
   
    On Error Resume Next
    For i = LBound(Combination) To UBound(Combination)
        Total = Total - Application.Combin(N - Combination(i), r + LBound(Combination) - i)
    Next i
    On Error GoTo 0
   
    GetIndexNumber = Total

End Function
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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