+ Reply to Thread
Results 1 to 4 of 4

Defined numbers - generator.

Hybrid View

  1. #1
    Registered User
    Join Date
    11-01-2012
    Location
    Romania
    MS-Off Ver
    Excel 2007
    Posts
    2

    Talking Defined numbers - generator.

    I have these 7 numbers: 1,3,3,3,4,5,9. I would need a generator that mixes up these numbers between them generating all possible combinations but not repeating the same combination so every combinations is unique.

    Ex: 1333459, 3334591, 3345913, 3459133, 4591333, 4315933 an so on...

    Thanks you for your time.

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,076

    Re: Defined numbers - generator.

    This should work

    Sub k1()
    Dim i, l, m, n As String
    Dim j, k As Integer
    m = ""
    i = "X1234567X"
    For j = 7 To 1 Step -1
    k = Int(Rnd() * j) + 1
    l = Mid(i, k + 1, 1)
    n = Left(i, k) & Right(i, Len(i) - (k + 1))
    Debug.Print i, k, l, n
    m = m + l
    i = n
    Next j
    Range("A1").Value = m
    End Sub
    Result is pasted into cell A1
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    Registered User
    Join Date
    11-01-2012
    Location
    Romania
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Defined numbers - generator.

    Unfortunately I get "The formula you typed contains an error.". Don't know what I'm doing wrong. Maybe you could attach the excel file to the reply?

    Thank you again for your time and effort!

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Defined numbers - generator.

    There are 840 unique permutations of the 7 values. This small macro, TEST, will list them in column A:

    Sub TEST()
    Dim j As Long, k As Long
    k = 1
    Application.ScreenUpdating = False
    For j = 1333459 To 9543331
        If NumCheck(CStr(j)) Then
            Cells(k, 1) = j
            k = k + 1
            ' If k = 1000 Then Exit Sub
        End If
    Next
    Application.ScreenUpdating = True
    End Sub
    Function NumCheck(s As String) As Boolean
    NumCheck = False
    If InStr(s, "0") + InStr(s, "2") + InStr(s, "6") + InStr(s, "7") + InStr(s, "8") = 0 Then
        If Len(Replace(s, "1", "")) = 6 And Len(Replace(s, "4", "")) = 6 And Len(Replace(s, "9", "")) = 6 Then
            If Len(Replace(s, "3", "")) = 4 Then
                NumCheck = True
                Exit Function
            End If
        End If
    End If
    End Function
    Macros are very easy to install and use:

    1. ALT-F11 brings up the VBE window
    2. ALT-I
    ALT-M opens a fresh module
    3. paste the stuff in and close the VBE window

    If you save the workbook, the macro will be saved with it.

    To remove the macro:

    1. bring up the VBE window as above
    2. clear the code out
    3. close the VBE window

    To use the macro from Excel:

    1. ALT-F8
    2. Select the macro (TEST)
    3. Touch RUN

    To learn more about macros in general, see:

    http://www.mvps.org/dmcritchie/excel/getstarted.htm

    Macros must be enabled for this to work!
    Last edited by Jakobshavn; 11-02-2012 at 10:28 AM. Reason: REMOVED BUG
    Gary's Student

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1