Skip to content

Instantly share code, notes, and snippets.

@mathew-kurian
Last active August 29, 2015 14:04
Show Gist options
  • Save mathew-kurian/de416af041345519c4b0 to your computer and use it in GitHub Desktop.
Save mathew-kurian/de416af041345519c4b0 to your computer and use it in GitHub Desktop.
VBA Useful Random Functions
Sub Test()
Debug.Print Random(0, 50)
Debug.Print RandomDateBetween(#1/1/2001#, #12/31/2001#)
Debug.Print RandomFrom(Array("cat", "dog"))
Debug.Print RandomDate
End Sub
Function RandomFrom(A As Variant) As String
RandomFrom = CStr(A(Random(0, UBound(A))))
End Function
Function RandomDate() As Date
RandomDate = Int(Rnd() * CDbl(Date + 1))
End Function
Function RandomDateBetween(LowerDate As Date, UpperDate As Date) As Date
RandomDateBetween = Int((UpperDate - LowerDate + 1) * Rnd + LowerDate)
End Function
Function Random(S As Integer, E As Integer) As Integer
Random = Int(Rnd * (E - S + 1)) + S
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment