Consulting

Results 1 to 4 of 4

Thread: Combining cell (nCr- Probability)

  1. #1

    Combining cell (nCr- Probability)

    Hi,
    I am searching a code for combine the cell in a column and display in another column.
    I would say combination set of all cell , please look in to the example below which is my requirement. I would need to find probability (Mathematically nCr , I am not sure ).


    example:
    column1
    NUT
    BOLT
    WASHER

    column2
    NUT
    NUT / BOLT
    NUT / WASHER
    NUT / BOLT / WASHER

    BOLT
    BOLT / NUT
    BOLT / WASHER
    BOLT / NUT / WASHER

    WASHER
    WASHER / NUT
    WASHER / BOLT
    WASHER / NUT / BOLT






    Thanks in advance.
    Last edited by nandakumar; 03-13-2008 at 08:20 AM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]


    Sub AllCombinations()
    Dim mpSize As Long
    Dim mpNextRow As Long
    Dim mpCarry As String
    Dim mpCallTree As Variant
    Dim i As Long

    mpSize = Cells(Rows.Count, "A").End(xlUp).Row

    For i = 1 To mpSize

    mpNextRow = mpNextRow + 1
    Cells(mpNextRow, "C").Value = Cells(i, "A").Value

    ReDim mpCallTree(1 To 1)
    mpCallTree(1) = i
    Call RecurseTree(Carry:=Cells(i, "A").Value, _
    Bound:=mpSize, _
    CallTree:=mpCallTree, _
    NextRow:=mpNextRow)
    Next i

    End Sub

    Private Function RecurseTree(ByVal Carry As String, _
    ByVal Bound As Long, _
    ByVal CallTree As Variant, _
    ByRef NextRow As Long) As Long
    Dim j As Long

    For j = 1 To Bound

    If IsError(Application.Match(j, CallTree, 0)) Then

    NextRow = NextRow + 1
    Cells(NextRow, "C").Value = Carry & " / " & Cells(j, "A").Value

    If j <= Bound Then

    ReDim Preserve CallTree(1 To UBound(CallTree) + 1)
    CallTree(UBound(CallTree)) = j
    Call RecurseTree(Carry:=Cells(NextRow, "C").Value, _
    Bound:=Bound, _
    CallTree:=CallTree, _
    NextRow:=NextRow)
    End If
    End If
    Next j
    End Function

    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Quote Originally Posted by xld
    [vba]


    Sub AllCombinations()
    Dim mpSize As Long
    Dim mpNextRow As Long
    Dim mpCarry As String
    Dim mpCallTree As Variant
    Dim i As Long

    mpSize = Cells(Rows.Count, "A").End(xlUp).Row

    For i = 1 To mpSize

    mpNextRow = mpNextRow + 1
    Cells(mpNextRow, "C").Value = Cells(i, "A").Value

    ReDim mpCallTree(1 To 1)
    mpCallTree(1) = i
    Call RecurseTree(Carry:=Cells(i, "A").Value, _
    Bound:=mpSize, _
    CallTree:=mpCallTree, _
    NextRow:=mpNextRow)
    Next i

    End Sub

    Private Function RecurseTree(ByVal Carry As String, _
    ByVal Bound As Long, _
    ByVal CallTree As Variant, _
    ByRef NextRow As Long) As Long
    Dim j As Long

    For j = 1 To Bound

    If IsError(Application.Match(j, CallTree, 0)) Then

    NextRow = NextRow + 1
    Cells(NextRow, "C").Value = Carry & " / " & Cells(j, "A").Value

    If j <= Bound Then

    ReDim Preserve CallTree(1 To UBound(CallTree) + 1)
    CallTree(UBound(CallTree)) = j
    Call RecurseTree(Carry:=Cells(NextRow, "C").Value, _
    Bound:=Bound, _
    CallTree:=CallTree, _
    NextRow:=NextRow)
    End If
    End If
    Next j
    End Function

    [/vba]
    hi,
    Please tell me how to use it. I am very new to macro

  4. #4
    hi,
    yes!! I got it ,, thanks a lot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •