totn Excel Functions

MS Excel: How to use the RANDOMIZE Function (VBA)

This Excel tutorial explains how to use the Excel RANDOMIZE function with syntax and examples.

Description

The Microsoft Excel RANDOMIZE function allows you to change the seed value used by the random number generator for the RND function.

The RANDOMIZE function is a built-in function in Excel that is categorized as a Math/Trig Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.

Syntax

The syntax for the RANDOMIZE function in Microsoft Excel is:

Randomize ( [ seed ] )

Parameters or Arguments

seed
Optional. It is a numeric seed that will be used by the RND function to generate a random number. If no seed value is provided, Excel will use the system timer as the seed value for the RND function.

Returns

The RANDOMIZE function does not return a value, but rather sets the seed value used by the RND function.

Applies To

  • Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • VBA function (VBA)

Example (as VBA Function)

The Randomize function can only be used in VBA code in Microsoft Excel.

Let's look at some Excel Randomize function examples and explore how to use the Randomize function in Excel VBA code:

For example:

'Example provided by techonthenet.com

Sub Macro1

   Dim LRandomNumber As Integer

   Randomize
   LRandomNumber = Int ((300 - 200 + 1) * Rnd + 200)

End Sub

In this example, the variable called LRandomNumber would now contain a random number between 200 and 300. The Randomize function would ensure that the number generated is truly random by initializing the random number generator with a seed value that is equivalent to the system timer.

Warning: If you don't call the Randomize function before calling the Rnd function, the Rnd function may return the same random number value each time. And therefore, you may not get a truly random number.