• Sep 18, 2025

Macro to Generate Random Numbers

  • Neale Blackwood
  • Macros
  • 0 comments

There are many ways to do this. This macro is based on a recorded macro that I edited.

Sign up to hear about free Excel training.

I won't share your email with anyone.

  • Reading time: 1 min read

I need to generate random numbers for my example data for blog posts and training manuals. This macro makes it quicker.

Select a range and run the macro. It will populate the range with random numbers from 1,000 to 10,000 formatted with the comma separator – see image below.

The macro that does the work is.

Sub RandomNumbers()

' generate random numbers in selected cells

    With Selection

        .Formula = "=RANDBETWEEN(1000,10000)"

        .Value = .Value

        .NumberFormat = "#,##0"

    End With

End Sub

You can edit the numbers in the RANDBETWEEN function in the code to change the range of random numbers.

This macro is useful if you are need values to test a formula or create examples.

0 comments

Sign upor login to leave a comment