QP School
Generate random numbers in VBA - Printable Version

+- QP School (https://qomplainerzschool.lima-city.de)
+-- Forum: Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=3)
+--- Forum: Excel VBA Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=48)
+--- Thread: Generate random numbers in VBA (/showthread.php?tid=5263)



Generate random numbers in VBA - Qomplainerz - 07-27-2023

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.