Results 1 to 3 of 3

Thread: [RESOLVED] Permutations?

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Resolved [RESOLVED] Permutations?

    I get confused between permutations and combinations, but anyhow I have ten columns, some of which may be blank while the rest contain one or two-digit numbers. Let's assume that the first row contains 4 numbers and that I can create this string "1,5,7,12" from those numbers. From that string, using VBA, I'd like to return these strings:
    0105
    0107
    0112
    0507
    0512
    0712

    If my string were "1,5,7,12,33" I'd like to return
    0105
    0107
    0112
    0133
    0507
    0512
    0533
    0712
    0733
    1233

    How would I do that?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,140

    Re: Permutations?

    Very rough, written in VB6, but there should be enough for you to work with for your own purposes.

    Code:
    Private Sub Command1_Click()
      MsgBox p("1,3,5,7")
    End Sub
    
    Private Function p(s As String) As String
      Dim a() As String
      Dim i As Integer
      Dim j As Integer
      Dim r As String
      
      a = Split(s, ",")
      
      For i = 0 To UBound(a) - 1
        For j = i + 1 To UBound(a)
          r = r & zp(a(i)) & zp(a(j)) & vbCrLf
        Next j
      Next i
      
      p = r
    
    End Function
    
    Private Function zp(s As String) As String
      If Len(s) = 1 Then
        zp = "0" & s
      Else
        zp = s
      End If
    End Function

  3. #3

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Permutations?

    Thanks, but I found my own relatively simple solution. strRangeText is the comma delimited string and strHits is an array.

    Code:
            Dim intStart As Integer
            Dim intNext As Integer
            Dim intCount As Integer
            Dim intMax As Integer
            strHits = Split(strRangeText, ",")
     
            intStart = 1
            
            intMax = UBound(strHits)
            For intCount = 0 To intMax
                For intNext = intCount + 1 To intMax
                    .Cells(lngNextRow, "BG") = Format(strHits(intCount), "00") & Format(strHits(intNext), "00")
                    lngNextRow = lngNextRow + 1
                    If intNext = intMax Then
                        intStart = intStart + 1
                    End If
                Next
            Next

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width