I have a macro that generates a set of random numbers from 1 to 8 in a table that progressively expands with each iteration (the Interval number in D3 determines the pause time). The problem is these numbers are not unique.
You'll see I'm using
What is the solution?
Number Sequence Loop.xlsb
This is the main procedure.
These are the three other procedures that the main one uses.
You'll see I'm using
id = Evaluate("LET(a,RANDARRAY(8),MATCH(a,SORT(a)))")
in my code. This formula generates a set of unique random numbers in a spilled cell range but not in the VBA code.What is the solution?
Number Sequence Loop.xlsb
This is the main procedure.
VBA Code:
Sub TableNumberSequence()
Dim tbl As ListObject
Dim rng As Range
Dim n As Integer
Set tbl = Worksheets("Sheet1").ListObjects("Table1")
Set rng = tbl.ListColumns("Number").DataBodyRange
Call DeleteAllRows
n = 1
Do While n < 9
Call IntervalTime
n = n + 1
AddRow n - 1
Loop
End Sub
These are the three other procedures that the main one uses.
VBA Code:
Sub AddRow(id)
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim tbl As ListObject
Set ws = Worksheets("Sheet1")
Set tbl = ws.ListObjects("Table1")
id = Evaluate("LET(a,RANDARRAY(8),MATCH(a,SORT(a)))")
Worksheets("Sheet1").ListObjects("Table1").ListRows.Add.Range.Cells(1).Value = id
Application.ScreenUpdating = True
End Sub
Sub DeleteAllRows()
With Range("Table1").ListObject
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.Delete
End If
End With
End Sub
Sub IntervalTime()
Dim target As Date
target = Now + TimeValue("00:00:" & Range("Interval").Text)
Do
DoEvents
Loop Until Now >= target
End Sub
Last edited: