Re: change of mind
Hello,
Yes. The way I generate Random Alpha Numeric Chars is with rndStr(), as seen below. The trick is to simply stack keyArr, your array holding the possible characters, with the appropriate characters, and you're in like Flynn.
To ensure X amount of Unique returns, you'll want some additional VBA in the form of a routine that populates an array of returns, first checking to make sure our return is not already in the array. For this example, foobar creates 100, unique, random returns, the upper boundary of the first vector of strArr:
<font face=Courier New><SPAN style="color:darkblue">Public</SPAN> <SPAN style="color:darkblue">Function</SPAN> rndStr(<SPAN style="color:darkblue">ByRef</SPAN> StrLength <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Long</SPAN>) <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">String</SPAN>
<SPAN style="color:darkblue">Dim</SPAN> b() <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Byte</SPAN>, keyArr() <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Byte</SPAN>
<SPAN style="color:darkblue">Dim</SPAN> i <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Long</SPAN>
<SPAN style="color:darkblue">Let</SPAN> keyArr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
<SPAN style="color:darkblue">ReDim</SPAN> b(1 <SPAN style="color:darkblue">To</SPAN> StrLength * 2)
<SPAN style="color:darkblue">For</SPAN> i = 1 <SPAN style="color:darkblue">To</SPAN> StrLength * 2 <SPAN style="color:darkblue">Step</SPAN> 2
<SPAN style="color:darkblue">Let</SPAN> b(i) = keyArr(Int(((UBound(keyArr) + 1) \ 2) * Rnd + 1) * 2 - 2)
<SPAN style="color:darkblue">Next</SPAN>
<SPAN style="color:darkblue">Let</SPAN> rndStr = b
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Function</SPAN>
<SPAN style="color:darkblue">Sub</SPAN> foobar()
<SPAN style="color:darkblue">Dim</SPAN> strArr(1 <SPAN style="color:darkblue">To</SPAN> 100, 1 <SPAN style="color:darkblue">To</SPAN> 1) <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">String</SPAN>
<SPAN style="color:darkblue">Dim</SPAN> tmpStr <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">String</SPAN>, i <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Long</SPAN>
<SPAN style="color:darkblue">For</SPAN> i = <SPAN style="color:darkblue">LBound</SPAN>(strArr) <SPAN style="color:darkblue">To</SPAN> <SPAN style="color:darkblue">UBound</SPAN>(strArr)
<SPAN style="color:darkblue">Let</SPAN> tmpStr = rndStr(8)
<SPAN style="color:darkblue">If</SPAN> IsNumeric(Application.Match(tmpStr, strArr, 0)) <SPAN style="color:darkblue">Then</SPAN>
<SPAN style="color:darkblue">Do</SPAN> <SPAN style="color:darkblue">Until</SPAN> <SPAN style="color:darkblue">Not</SPAN> IsNumeric(Application.Match(tmpStr, strArr, 0))
<SPAN style="color:darkblue">Let</SPAN> tmpStr = rndStr(8)
<SPAN style="color:darkblue">Loop</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
<SPAN style="color:darkblue">Let</SPAN> strArr(i, 1) = tmpStr
<SPAN style="color:darkblue">Next</SPAN>
<SPAN style="color:darkblue">Let</SPAN> Range("a1").Resize(UBound(strArr, 1)).Value = strArr
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>
To generate more or less returns, change the upper boundary of strArr from 100 to whatever. And the example places the returns in A1:AX, where X is also the upper boundary of strArr. To change the top cell as to where this array gets dumped, change A1 to wherever you please.
Hope this helps.