Your question, your audience. Choose who sees your identityâ€”and your questionâ€”with question security.
Option Explicit
Sub Randomish()
Dim xMin As Double
Dim xActual As Double
Dim xTarget As Double
Dim i As Long
Sheets("Sheet2").Activate
xTarget = Range("B1")
xMin = 100000000
Application.ScreenUpdating = False
For i = 1 To 100
Range("B6:F105").Formula = "=(RANDBETWEEN($B$3,$B$4)+(RANDBETWEEN(0,9)/100))"
Range("B6:F105").Copy
Range("B6:F105").PasteSpecial xlPasteValues
xActual = Range("H1")
If Abs(xTarget - xActual) / xTarget < 0.01 Then
MsgBox ("Run complete - difference is " & Format(Abs(xTarget - xActual) / xTarget, "0.00%"))
Exit Sub
End If
If Abs(xTarget - xActual) < xMin Then xMin = Abs(xTarget - xActual)
Next
MsgBox ("Sorry - couldn't get within reach. Closest was " & Format(xMin / xTarget, "0.00%") & ". Please try again.")
Application.ScreenUpdating = True
End Sub
Regards,
Sample.xlsx