The following function generates random data using a format string that you can customize.
' Note that a token contained within a second token (e.g. yy to yyyy) must be placed AFTER the containing token (yyyy must precede yy)
FormatArray = Array("#", "DAY", "MONTH", "dd", "mm", "yyyy", "yy", "NAME", "COLOR", "CAR")
' Definition of formats not appearing as a Case in GetRandStrForToken
Array("DAY", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"),_
Array("MONTH", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),_
Array("COLOR", "Red", "Yellow", "Blue", "Green"),_
Array("NAME", "Hilik", "Oren", "Shoshi", "Ilan"),_
Array("CAR", "Ford", "
' Generate a random value for a specific token
Function GetRandStrForToken(sToken)
GetRandStrForToken = CStr(Int(Rnd*10))
GetRandStrForToken = CStr (Int(Rnd*31 + 1))
GetRandStrForToken = CStr (Int(Rnd*12 + 1))
GetRandStrForToken = CStr(Int(Rnd*10)) + CStr(Int(Rnd*10))
GetRandStrForToken = CStr (1950 + Int(Rnd * 100))
Case Else ' Pick from the Values Array for other formats
For i = 0 to UBound(ValuesArray)
If sToken = ValuesArray(i)(0) Then
GetRandStrForToken = ValuesArray(i)( 1 + Int( rnd*(UBound(ValuesArray(i))) ) )
' Parse the current token (if any)
Function GetNextToken (ByRef sFormat)
For each sToken in FormatArray
sTemp = Mid(sFormat, 1, Len(sToken))
sFormat = Mid(sFormat, Len(sToken) + 1)
' Generate random data given a format
Function GenerateRandData(ByVal Format)
' Anything inside a [] brackets is copied as is
If Mid(Format, 1, 1) = "[" Then ' Find the closing brackets
nPos = InStr(1, Format, "]", vbTextCompare)
If nPos = 0 Then Exit Function
GenerateRandData = GenerateRandData + Mid(Format, 2, nPos - 2)
Format = Mid(Format, nPos + 1)
GenerateRandData = GenerateRandData + GetRandStrForToken(sToken)
Else ' No token - just copy the current character
GenerateRandData = GenerateRandData + Mid(Format,1, 1)
msgbox GenerateRandData ("Today Is the first DAY in MONTH")
msgbox GenerateRandData ("A Date in the format [dd/mm/yyyy] dd/mm/yyyy")
msgbox GenerateRandData ("My name is NAME, I drive a COLOR CAR. you can reach me on +972-54-###-####")
No comments:
Post a Comment