Let’s say you want to print some random numbers in a selected range then you can use below Macro.
Sub Print_Randomnumbers()
Dim myrng As Range
For Each myrng In Selection
myrng.Value = Application.WorksheetFunction.Int(Rnd * 1000)
Next myrng
End Sub
How it works?
- It's a for each loop which loops through each cell in the selection.
- VBA function Rnd populate random number from 0 to 1.
- If you want to print numbers up to the range of 1000 then multiply Rnd with 1000.
- . Int function pulls the integer value from Rnd*1000.