ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Fast way to generate unique strings (https://www.excelbanter.com/excel-programming/314769-fast-way-generate-unique-strings.html)

R Avery

Fast way to generate unique strings
 
what is the fastest VBA code to generate a list of random strings of a
specified length.

the function prototype i want is

sub GetUniqueStrings(Strings() as string, Length as long)


The one i had been using seems slow when using it to generate lots of
strings, and i was wondering if there was a way to speed it up. Thanks!




Public Function GetUniqueString(ByVal NumChars As Long) As String
Dim i As Long, str As String

GetUniqueString = Space$(NumChars)

For i = 1 To NumChars
Mid$(GetUniqueString, i, 1) = Chr(RandBetween(97, 122))
Next i

End Function

Public Function RandBetween(ByVal LB As Long, ByVal UB As Long) As Long
RandBetween = LB + Round(Rnd * (UB - LB), 0)
End Function

Doug Glancy

Fast way to generate unique strings
 
I'm curious to see what you've been using.

Doug Glancy

"R Avery" wrote in message
...
what is the fastest VBA code to generate a list of random strings of a
specified length.

the function prototype i want is

sub GetUniqueStrings(Strings() as string, Length as long)


The one i had been using seems slow when using it to generate lots of
strings, and i was wondering if there was a way to speed it up. Thanks!




Public Function GetUniqueString(ByVal NumChars As Long) As String
Dim i As Long, str As String

GetUniqueString = Space$(NumChars)

For i = 1 To NumChars
Mid$(GetUniqueString, i, 1) = Chr(RandBetween(97, 122))
Next i

End Function

Public Function RandBetween(ByVal LB As Long, ByVal UB As Long) As Long
RandBetween = LB + Round(Rnd * (UB - LB), 0)
End Function




JE McGimpsey

Fast way to generate unique strings
 
Don't know about the fastest, but this returns 100 strings of 100
characters in the blink of an eye...

Public Function GetUniqueStrings( _
ByRef Strings() As String, Length As Long)
Dim i As Long
Dim j As Long
For i = LBound(Strings) To UBound(Strings)
Strings(i) = Space(Length)
For j = 1 To Length
Mid(Strings(i), j, 1) = Chr(Int(97 + Rnd() * 26))
Next j
Next i
End Function

Called as:

Public Sub try()
Dim s(1 To 100) As String
GetUniqueStrings s, 100
Range("A1").Resize(UBound(s)).Value = Application.Transpose(s)
End Sub



In article ,
R Avery wrote:

what is the fastest VBA code to generate a list of random strings of a
specified length.

the function prototype i want is

sub GetUniqueStrings(Strings() as string, Length as long)



All times are GMT +1. The time now is 01:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com