Results 1 to 8 of 8

Thread: Generate random number of strings that are stored in an array .

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,887
    Location
    Quote Originally Posted by wannabe11 View Post
    thanks for your answers guys , i've came across an example online and i modified the code a bit . it seems to work fine .
    still i dont understand the role of the randomize and how the int function solve the issue since this function only bring whole numbers .
    , each one generated being unique.
    That macro doesn't guarantee uniqueness

    Option Explicit
    
    Const N As Long = 10000
    Const strLength As Long = 8
    Const strChars As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    
    Sub UniqueStrings()
        Dim r As Long
        Dim s As String, s1 As String
        Dim v As Variant
        Dim i As Long
        
        'add first string and a seperation char
        s = pvtMakeString(strLength, strChars) & "#"
        
        For i = 2 To N
            'make another
            s1 = pvtMakeString(strLength, strChars)
            
            'check if already in the string list
            Do While InStr(s, s1) > 0
                'if it is try another until it's not
                s1 = pvtMakeString(strLength, strChars)
            Loop
            
            s = s & s1 & "#"
        Next i
        
        'split string at the #
        v = Split(s, "#")
        
        For i = 0 To N - 1
            Cells(i + 1, 1) = v(i)
        Next i
    End Sub
    
    
    Private Function pvtMakeString(L As Long, C As String) As String
        Dim i As Long
        Dim r As Long
        Dim s As String
        
        For i = 1 To L
            r = Int(Len(strChars) * Rnd + 1)
            s = s & Mid(C, r, 1)
        Next i
        pvtMakeString = s
    End Function
    Last edited by Paul_Hossler; 10-11-2017 at 01:12 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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