Tuesday, December 11, 2018

If my input is 1,2 & 3 then result should be 1,2,3,12,13,23,123

Q. Need VBA code to get different combinations. Eg. if my input is 1 & 2 then the result should be 1,2,12.
If my input is 1, 2 & 3 then result should be 1,2,3,12,13,23,123

Input can go up to 20 digits also
Also, result 12 and 21 should treat as same
Similarly, 123 & 321 or 231 or 312 is the same.




here is the perfect dynamic solution. You can add more than 20 digits and it will still provide you with the perfect result.

Put the following code in the module and that's it. I was working on this since yesterday. :D




Sub Perform_Combination_Series_Dynamic()

    Dim InputString, MainString, PartS As String
    Dim I, J, K, L, M, Group As Long
    Dim InputStringDigits() As String 'Array
    
    InputString = Application.InputBox("Please Enter a digits seperated by comma")
    
    InputStringDigits = VBA.Split(InputString, ",")
    
    MainString = vbNullString
    I = UBound(InputStringDigits) - LBound(InputStringDigits) + 1
    
    For J = 1 To I Step 1
        MainString = MainString & "," & J
    Next J

    For Group = 0 To I - 2
        For J = 1 To I
            For K = J + 1 To I
                If K + Group <= I Then
                    PartS = vbNullString
                    For M = 0 To Group
                        PartS = PartS & K + M
                    Next M
                    MainString = MainString & "," & J & PartS
                End If
            Next K
        Next J
    Next Group
    
    MsgBox VBA.Mid(MainString, 2, VBA.Len(MainString))
    
End Sub

No comments:

Post a Comment

IsValidPasswordString Function

'Following function will verify if the password string contains following characters or not? Rem : List of Characters Group - ASCII Rem ...