Results 1 to 5 of 5

Troubleshooting - Prevent Duplicate Entries From VBA Code

Threaded View

  1. #1
    Registered User
    Join Date
    07-13-2015
    Location
    Chicago, IL
    MS-Off Ver
    2013
    Posts
    14

    Troubleshooting - Prevent Duplicate Entries From VBA Code

    I have some VBA code that I had some help building to generate a random 5-character code with the format: letter-number-letter-number-letter (e.g. d6s8b). The code seems to be working just fine but there should be 1.7 million options and we've created roughly 6,000 codes and we have already come across 8 duplicate codes that were generated. I thought the code would be preventing that, so I'm not sure why this is happening. Anyone's help would be greatly appreciated!

    Our code is below:
    Sub GenerateUniqueRandomValue()
    
      Dim ws As Worksheet
    
      Dim r As Range
      Dim iLastRow As Long
      Dim x1 As Long
      Dim x2 As Long
      Dim x3 As Long
      Dim x4 As Long
      Dim x5 As Long
      Dim bNeedMore As Boolean
      Dim sValue As String
      
      'ASCII Table Reference: http://www.asciitable.com/
      
    
      'Create the Worksheet object
      Set ws = ThisWorkbook.Sheets("Master Code Generator")
    
      
      'Use the System Timer to Generate the first Random Number
      Call Randomize
      
      'Loop until there is a unique Value
      bNeedMore = True
      While bNeedMore = True
      
      
        'Generate a random number between 0 and 25
        'Create the numerical equivalent of an ASCII character between 'a' and 'z'
        x1 = Int(26 * Rnd()) + Asc("a")
    
        'Generate a random number between 0 and 9
        x2 = Int(10 * Rnd())
    
        'Generate a random number between 0 and 25
        'Create the numerical equivalent of an ASCII character between 'a' and 'z'
        x3 = Int(26 * Rnd()) + Asc("a")
    
        'Generate a random number between 0 and 9
        x4 = Int(10 * Rnd())
    
        'Generate a random number between 0 and 25
        'Create the numerical equivalent of an ASCII character between 'a' and 'z'
        x5 = Int(26 * Rnd()) + Asc("a")
    
        'Create the entire string
        sValue = Chr(x1) & x2 & Chr(x3) & x4 & Chr(x5)
        Debug.Print sValue
      
      
        'Make sure the value is unique in Column 'A'
        'Find the first occurence of the string in Column 'A'
        Set r = Nothing
        Set r = ws.Columns("A").Find(What:=sValue, _
                            After:=ws.Range("A1"), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False, _
                            SearchFormat:=False)
                          
                          
        'Exit if the value is unique
        'Otherwise try again
        If r Is Nothing Then
          bNeedMore = False
        End If
     
      Wend
     
     'Find the last item in Column 'B' (must be row 1 or more)
     'Runtime error is generated if the column is Empty
     On Error Resume Next
     iLastRow = ws.Range("B:B").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
     If iLastRow < 1 Then
       iLastRow = 1
     End If
     On Error GoTo 0
     
     'Put the value in Column 'B' in the next row
     ws.Cells(iLastRow + 1, "B").Value = sValue
     
     
     'Clear object pointers
     Set r = Nothing
     Set ws = Nothing
      
    End Sub
    Last edited by mcodden; 05-17-2016 at 11:24 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Prevent duplicate entries
    By Blokeman in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-06-2015, 07:24 PM
  2. Prevent duplicate entries in Data Validation List By code
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-26-2014, 04:56 PM
  3. Prevent duplicate entries in Data Validation List By code
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-26-2014, 04:37 PM
  4. Code to prevent duplicate entries
    By pipoliveira in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-17-2014, 08:49 AM
  5. [SOLVED] Deleting Duplicate Row code troubleshooting
    By ds16 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-07-2012, 04:31 PM
  6. Replies: 0
    Last Post: 06-14-2012, 12:38 PM
  7. code to prevent duplicate entries
    By cart0250 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-19-2006, 02:44 PM

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