[VBA&code] Adding controls to an existing code.

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
VBA Code:
Sub Monte_carlo_Mr_excel()
On Error GoTo errHandler

Dim rRng As Range, p As Integer, lRow As Long
Dim vElements, vresult As Variant
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
lRow = 0
Set rRng = Range("A1:A" & lastRow)
rRng.Select
p = Range("C19")
If CInt(Range("C19")) = 0 Then
MsgBox "Enter amount of numbers to be in combinations"
Exit Sub
End If

 Dim q As Integer
          Dim b As Double
          b = 1
          For q = 0 To p - 1
                    b = b * (lastRow - q) / (p - q)
          Next q
MsgBox "No of Non repeating combinations is -> " & b, vbInformation
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
ReDim vresult(1 To p)
Columns("D").Resize(, p + 15).Clear
Call CombinationsNP(vElements, p, vresult, lRow, 1, 1)
Exit Sub
errHandler:
MsgBox "Error has occured - error no " & Err.Number & " - " & Err.Description
End Sub


Sub CombinationsNP(vElements As Variant, p As Integer, vresult As Variant, lRow As Long, iElement As Integer, iIndex As Integer)
Dim i As Integer, k As Integer
On Error GoTo errHandler
For i = iElement To UBound(vElements)
    vresult(iIndex) = vElements(i)
    If iIndex = p Then
        For k = LBound(vresult) To UBound(vresult)
               Next
        lRow = lRow + 1
        Range("D" & lRow).Resize(, p) = vresult
       End If
       testRow = testRow + 1
       If iIndex <> p Then
        Call CombinationsNP(vElements, p, vresult, lRow, i + 1, iIndex + 1)
        End If
Next i

Exit Sub
errHandler:
MsgBox "Error has occured - error no " & Err.Number & " - " & Err.Description
End Sub
what I get from here is;
ZMUSOEN choose from 3 to 6.xlsm
ABCDEF
1008
2805
3502
4207
5701
6103
704
809
985
1082
1187
1281
1383
1484
1589
1652
1757
1851
19Enter Number in Combination - 253
2054
2159
2227
2321
2423
2524
2629
2771
2873
29COUNT ELEMENT FREQ.74
30179
31113
32114
33119
34134
35139
36149
371
381
391
401
411
421
431
44
45
Combination Generation
Cell Formulas
RangeFormula
B30B30=COMBIN(COUNT($A$2:$A$16),D1)
B31:B43B31=COMBIN(COUNT($A$2:$A$16),C31)
Named Ranges
NameRefers ToCells
'Combination Generation'!_FilterDatabase='Combination Generation'!$A$2:$A$461B30:B43

from here I would like to have control on
how many time each element must be in the comb.
and how namy comb. I want.
like this:

ZMUSOEN choose from 3 to 6.xlsm
ABCDE
10element freq.608
28505
35402
42407
57301
61309
7904
8485
982
1087
1181
1289
1384
1452
1557
1651
1759
1854
19enter number of digist227
20enter number of comb.1321
2129
2224
2371
2479
2574
2619
2714
2894
29COUNT ELEMENT FREQ.
3010
31#NUM!8
32215
33212
3417
3571
361
371
381
391
401
411
421
431
44
45
Combination Generation
Cell Formulas
RangeFormula
B30:B43B30=COMBIN(COUNT($A$2:$A$16),C30)
Named Ranges
NameRefers ToCells
'Combination Generation'!_FilterDatabase='Combination Generation'!$A$2:$A$461B30:B43

in front of 0 on C is 6 meaning I would like 0 to show up 6 times etc.
Thank you for your time,
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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