Sub Q2_Ans()
Dim ar, cRng As Range, rng As Range
Dim N As Integer, X As Integer, Y As Integer
Dim r As Integer, c As Integer
Application.ScreenUpdating = False
Sheets("Sheet1").Activate
Arr = Range("Parameters")
N = Arr(1, 2)
X = Arr(2, 2)
Y = Arr(3, 2)
Z = (N * Y) / X
If Z = Int(Z) Then
maxn = Z
Else
maxn = Int(Z + 1)
End If
Range("B3:H50").ClearContents
srow = 3
lrow = srow + N - 1
Set rng = Range("B3:G" & lrow)
idx = 1
For r = srow To lrow
Cells(r, 2) = idx
idx = idx + 1
Next r
For r = srow To lrow
For j = 1 To Y
nextc:
If Cells(r, 8) >= 1 Then
minv = WorksheetFunction.Min(Range("colN"))
c = Application.Match(minv, Range("Coln"), 0) + 2
If Cells(r, c) <> "" Then c = WorksheetFunction.RandBetween(1, X) + 2
Else
c = WorksheetFunction.RandBetween(1, X) + 2
End If
If Cells(r, c) <> "" Then GoTo nextc
Set cRng = Range(Cells(srow, c), Cells(lrow, c))
nx = Application.CountIf(cRng, "X")
If nx >= maxn Then GoTo nextc
Cells(r, c) = "X"
Cells(r, 8) = Cells(r, 8) + 1
Next j
If Range("Finish") = N Then Exit For
Next r
Application.ScreenUpdating = True
End Sub
Bookmarks