QP School

Full Version: Generate random numbers in VBA
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Example:

Sub GenerateRandomNumbers()
    Dim i As Long

    'Generate and place 10 random numbers in column A
    For i = 1 To 10
        Cells(i, "A").Value = Int((100 * Rnd) + 1)
    Next i
End Sub

Explanation:

The code defines a subroutine named GenerateRandomNumbers.
It declares a loop counter i.
The For loop runs from 1 to 10, generating ten random numbers.
Rnd is used to generate a random number between 0 and 1. 
(100 * Rnd) generates a random number between 0 and 100, 
and Int((100 * Rnd) + 1) generates a random integer between 1 and 100.
The generated random number is placed in column A for each iteration using Cells(i, "A").Value.