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
.
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 theicon 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!)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks