Function String Manipulations Class ★ Top 20 Most Useful

HashString

Return a hash value for the specified string. Converts a string into a numeric identifier that is likely unique, given an appropriate value for vRangePrimeNumber. The number returned will be between 0 (zero) and vRangePrimeNumber - 1.

Signature (Visual Basic / VBA)

Public Function HashString( _
      ByVal vValue As Variant _
    , Optional ByVal vRangePrimeNumber As Variant _
    ) As Variant

Code Example

    HashString("Hello") = 14802676
    HashString("Hello", 4093) = 83
    HashString("World") = 10825270
    HashString("World", 4093) = 1445

Parameters

Parameter Description
vValue The string which is to be "hashed." Function returns Null if vValue is Null or cannot be fixed up to a String.
vRangePrimeNumber Number which determines the range of values returned from the hash operation. vRangePrimeNumber defaults to 16,777,213 (a suitably large prime number) when it is missing or Null or cannot be interpreted as a number. vRangePrimeNumber should be a positive, prime number. Returns the hash value as a double-precision floating-point number (a Double) that will be an integer.

Notes & Version History

Summary Given the same string (in vValue) and vRangePrimeNumber, this function will always return the same "hash" value. The number of strings with the same hash value depends upon the value of vRangePrimeNumber and the number of strings that are "hashed." If the number of (unique) strings hashed exceeds vRangePrimeNumber, then there will definitely be duplicate hash values generated.
Note All character comparisons are Binary so, for example, HashString("H") <> HashString("h")