Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel Macro to Generate Random Numbers: How-To

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can generate random numbers in Excel with a short VBA macro that fills a range with values from the built-in Rnd function. Open the workbook, press Alt+F11 to open the Visual Basic Editor, insert a module, paste a macro that loops through the cells you want and writes Rnd-based values into them, then run it and save the file as an .xlsm macro-enabled workbook. The Rnd function returns a decimal between 0 and 1, and you wrap it in Int, Round or a formula to get whole numbers or a specific range.

What You'll Need

  • Excel on Windows or Mac with macros enabled. The menu labels and keyboard shortcuts differ slightly by version and region, so the exact wording you see may not match these steps word for word.
  • A workbook you are allowed to save in the macro-enabled .xlsm format. If the file is shared read-only or sits in a protected folder, copy it somewhere you can write first.
  • Basic awareness of how to open the Visual Basic Editor: Alt+F11 on Windows, or the Developer tab on either platform.
  • If the Developer tab is not visible, enable it via File > Options > Customize Ribbon (Windows) or Excel > Preferences > Ribbon & Toolbar (Mac).

Step-by-Step

  1. Save a copy of your workbook. Macros overwrite cell contents, and a mistake can wipe data. Save the original first so you can restore it.
  2. Open the Visual Basic Editor. On Windows press Alt+F11. On Mac, use Tools > Macro > Visual Basic Editor from the menu bar. A separate editor window opens.
  3. Insert a standard module. In the editor choose Insert > Module. A blank code pane appears. Do not paste code into the sheet's own code module unless you intend to trigger it from an event like opening the workbook.
  4. Paste a macro for decimals. The simplest version fills a range with random decimals between 0 and 1:
    Sub FillRandomDecimals()
        Dim cel As Range
        For Each cel In Selection
            cel.Value = Rnd
        Next cel
    End Sub
    This writes a different decimal into every selected cell.
  5. Paste a macro for whole numbers in a range. To get integers between a low and high value, use Int with a width:
    Sub FillRandomIntegers()
        Dim cel As Range
        Dim lowVal As Long, highVal As Long
        lowVal = 1
        highVal = 100
        For Each cel In Selection
            cel.Value = Int((highVal - lowVal + 1) * Rnd + lowVal)
        Next cel
    End Sub
    Change lowVal and highVal to suit your case. The +1 is essential; without it the high value never appears.
  6. Return to your sheet and select the target cells. Both macros act on whatever is selected, so highlight the exact range you want filled before running anything.
  7. Run the macro. Back in Excel, press Alt+F8 to open the macro list, pick FillRandomDecimals or FillRandomIntegers, and click Run. On Mac use Tools > Macro > Macros. The selected cells fill instantly.
  8. Stop the values from changing. Rnd recalculates only when the macro runs again, but if you need a fixed snapshot, copy the filled cells and use Paste Special > Values over the same range, or copy them to a new sheet. This locks the numbers in place.
  9. Seed the generator if you want repeatable output. Add Randomize before the loop to reseed from the system clock, or use Randomize 42 with a fixed number to reproduce the same sequence each time. Without Randomize, Excel uses the same starting seed every session, so a fresh workbook can produce an identical sequence.
  10. Save as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A plain .xlsx save strips the macro out silently, and you will lose it next time you open the file.

Common Mistakes

  • Forgetting the +1 in the integer formula. Int((high - low) * Rnd + low) never returns the top value. Always use (high - low + 1).
  • Omitting Randomize. The Rnd sequence repeats across sessions unless you reseed it, which looks like the macro is broken when it is not.
  • Saving as .xlsx. Excel warns you that macros cannot be saved; if you click through, the code is gone. Save as .xlsm from the start.
  • Running with the wrong selection. Selecting a whole column instead of a fixed range fills thousands of cells and can be slow or overwrite data below your table.
  • Leaving duplicate values when you need unique ones. Rnd can and will repeat numbers. If uniqueness matters, test each new value against those already written and regenerate on a clash.
  • Pasting code into the wrong module. Code in a worksheet module only fires on that sheet's events. Standard modules are for macros you run manually.

Tips

  • For random numbers that behave like a normal distribution rather than a flat spread, use WorksheetFunction.Norm_Inv(Rnd(), mean, stdDev) instead of a plain range formula.
  • To shuffle a list, fill a helper column with random decimals beside your data, then sort by that column and delete it afterwards.
  • You can skip VBA entirely for one-off work: =RANDBETWEEN(1,100) gives integers and =RAND() gives decimals. Both recalculate on every sheet change, so paste as values to freeze them. The macro is better when you need to fill many cells at once or lock results immediately.
  • Assign the macro to a button via Developer > Insert > Button so you can rerun it with one click.
  • Test on a spare sheet first. Macros cannot be undone with Ctrl+Z once they have written values.

Troubleshooting

  • Nothing happens when you run it. Check that cells are selected and that the sheet is not protected. A protected sheet rejects writes silently in some versions; unprotect it via Review > Unprotect Sheet.
  • You get a security warning or the macro is blocked. Excel blocks macros from files downloaded from the internet. Open the file's properties and look for the unblock option, or move the file to a trusted location. Only enable macros in files you created or trust.
  • The Developer tab is missing. Enable it through the ribbon customisation options mentioned above; Alt+F11 still works on Windows even without the tab.
  • You see a compile error. Usually a pasted line lost its line break, or the code landed outside a Sub/End Sub pair. Re-paste the full block cleanly.
  • The numbers repeat every time. Add Randomize at the top of the macro, or vary the seed argument.
  • The file opens with the macro gone. It was saved as .xlsx. Re-add the code and save as .xlsm.

Frequently Asked Questions

Can I generate random numbers without a macro?

Yes. RAND and RANDBETWEEN are built-in functions. They recalculate whenever the sheet changes, so copy and paste as values if you need them fixed.

How do I get unique random numbers?

Loop until each generated value is not already present in the range, or fill a column with RAND, sort it, and take the top however many you need.

Why does my macro give the same numbers each session?

Rnd uses a default seed until you call Randomize. Adding Randomize reseeds from the clock and changes the sequence.

Can I control the range of decimals, not just integers?

Yes. Multiply Rnd by the width and add the low value, for example lowVal + (highVal - lowVal) * Rnd, which gives decimals between the two bounds.

Will the macro work on Mac Excel?

Yes, VBA runs on both platforms, though the menu paths and shortcut keys differ.

Does Rnd produce truly random numbers?

No. It is a pseudo-random generator, fine for simulations, sampling and test data, but not suitable for anything requiring cryptographic randomness such as security keys.