+ Reply to Thread
Results 1 to 2 of 2

Thread: Excel and arrays

  1. #1
    Registered User
    Join Date
    03-09-2011
    Location
    vancouver
    MS-Off Ver
    Excel 2003
    Posts
    1

    Excel and arrays

    I need to do the following:

    1. Select a cell in column I
    2. Press f12-->input box asks for two letter code
    Loop
    3 User enters a two letter code-->excel generates a random number between 1 and 100
    4 Excel appends the inputted two letter code to the random integer
    eg random integer=57 and inputted code is "CM"
    --->result is CM57
    5 Excel searches all of column I for CM57
    .6 If found, then loop back to step 3
    Until the result is NOT found in column I
    Populate current cell with the result ie CM57
    .

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,228

    Re: Excel and arrays

    Try this:
    Option Explicit
    
    Sub RandomCode()
    Dim MyCode As String
    Dim MyStr As String
    Dim chkCnt As Long
    Dim chkFIND As Range
    
    StartAgain:
    chkCnt = 0
    MyCode = UCase(Application.InputBox("Please enter a two letter code", "Your code", "AB", Type:=2))
    If Len(MyCode) <> 2 Then
        If MsgBox("Two letter code not entered. Abort?", vbYesNo, "Abort query") = vbYes Then
            Exit Sub
        Else
            GoTo StartAgain
        End If
    End If
    
    On Error Resume Next
    Do
        MyStr = MyCode & [randbetween(1,100)]
        Set chkFIND = Columns(ActiveCell.Column).Find(MyStr, LookIn:=xlValues, LookAt:=xlWhole)
        If chkFIND Is Nothing Then
            ActiveCell = MyStr
            Exit Sub
        End If
        chkCnt = chkCnt + 1
    Loop Until chkCnt > 200
    
    If MsgBox("Code entered failed after 200 random attempts. Try a new code?", vbYesNo, "Try Again") _
        = vbYes Then GoTo StartAgain
    
    End Sub
    Last edited by JBeaucaire; 03-09-2011 at 08:03 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0