Public Function RandBetweenA(ByVal dblLower As Double, _
ByVal dblUpper As Double, _
Optional lngDecimals As Long = 5, _
Optional blnVolatile As Boolean = False) As Variant()
Dim rngArea As Range, lngItem As Long, lngRow As Long, lngCol As Long
Dim varResult As Variant, varResults() As Variant, varTemp() As Variant
Dim lngMaxIterations As Long
If blnVolatile Then Application.Volatile
If StrComp(TypeName(Application.Caller), "Range", vbBinaryCompare) <> 0 Then
Call Err.Raise(Number:=vbObjectError + 1024, Description:="RandbetweenA is only callable from a range!")
Exit Function
End If
lngMaxIterations = dblUpper * (10 ^ lngDecimals) - dblLower * (10 ^ lngDecimals) + 1
Set rngArea = Application.Caller
ReDim varResults(1 To rngArea.Rows.Count, 1 To rngArea.Columns.Count)
ReDim varTemp(1 To rngArea.Count)
For lngRow = 1 To rngArea.Rows.Count
For lngCol = 1 To rngArea.Columns.Count
lngItem = lngItem + 1
If lngItem <= lngMaxIterations Then
Do
varResult = Evaluate("rand()*(" & dblUpper & "-" & dblLower & ")+" & dblLower)
varResult = Round(varResult, lngDecimals)
Loop Until IsError(Application.Match(varResult, varTemp, 0))
Else
varResult = CVErr(xlErrNum)
End If
varTemp(lngItem) = varResult
varResults(lngRow, lngCol) = varResult
Next lngCol
Next lngRow
RandBetweenA = varResults
End Function