Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generate random numbers in VBA
#1
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.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Autofill a series of numbers in a column Qomplainerz 0 388 07-27-2023, 11:23 AM
Last Post: Qomplainerz
  Loop to fill numbers in cells with VBA Qomplainerz 0 379 07-27-2023, 08:16 AM
Last Post: Qomplainerz
  Calculate the average of three numbers in VBA Qomplainerz 0 322 07-27-2023, 07:40 AM
Last Post: Qomplainerz
  Loop to display numbers in VBA Qomplainerz 0 330 07-27-2023, 07:33 AM
Last Post: Qomplainerz
  Floating-point numbers with double precision in VBA Qomplainerz 0 301 07-26-2023, 08:23 PM
Last Post: Qomplainerz
  Floating-point numbers with single precision in VBA Qomplainerz 0 268 07-26-2023, 08:23 PM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 8 Guest(s)