VBA & Macro Problems

Build a VBA Array with Random and Rank Columns in Excel

Huda QurayshiHuda Qurayshi Nov 25, 2026 868 views

Question details

Create a multidimensional VBA array containing a configurable sequence, random numbers, and ranks for those random numbers.

Build a VBA Array with Random and Rank Columns in Excel
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A user has VBA code that populates an array from row count, interval, and starting-value variables and wants to add random and rank columns.
Observed behavior
The desired array has one row per configured value, with separate columns for the base value, a random integer, and its rank.
Before you start

Open the workbook in a version of Excel that supports VBA and enable the Developer tab if it is hidden. Choose an empty area on the active worksheet for the output; the example writes to cells A1 through C11.

Solution 1Recommended

Populate and rank a three-column VBA array

Use this macro to generate a configurable sequence and rank random integers without relying on a worksheet ranking function.

The first column contains the sequence, the second contains random integers from 1 through 100, and the third ranks those integers in descending order. Equal random values receive the same rank, with gaps after ties.

1
Open a VBA module

In Excel, select Developer > Visual Basic, then choose Insert > Module in the VBA editor.

2
Add the array macro

Paste this code into the module: Sub BuildRandomRankArray() Dim data() As Variant Dim rowCount As Long, interval As Long, startValue As Long Dim i As Long, j As Long, rankValue As Long rowCount = 10 interval = 5 startValue = 1 If rowCount < 1 Then Exit Sub ReDim data(1 To rowCount, 1 To 3) Randomize For i = 1 To rowCount data(i, 1) = startValue + (i - 1) * interval data(i, 2) = Int(Rnd * 100) + 1 Next i For i = 1 To rowCount rankValue = 1 For j = 1 To rowCount If data(j, 2) > data(i, 2) Then rankValue = rankValue + 1 Next j data(i, 3) = rankValue Next i With ActiveSheet .Range("A1").Value = "Base value" .Range("B1").Value = "Random number" .Range("C1").Value = "Rank" .Range("A2").Resize(rowCount, 3).Value = data End With End Sub

3
Set the variables and run

Change rowCount, interval, and startValue in the module as needed. Select the target worksheet, then choose Developer > Macros, select BuildRandomRankArray, and click Run.

Populate and rank a three-column VBA array
Output and ties: The macro overwrites A1:C11 with its default settings; a larger rowCount extends the output downward. Duplicate random numbers can occur and receive the same rank.
Work with spreadsheet macros

Build the random-rank array in WPS Spreadsheets

In a WPS Office installation with VBA support, use the macro above to generate the array and write its values and ranks to a worksheet. VBA availability and compatibility vary by version and installation.

  1. 1. Open the workbook: Open WPS Spreadsheets and select File > Open to choose your workbook.
  2. 2. Check VBA support: Look for Developer > Visual Basic. If it is unavailable, check whether your WPS version and installation support VBA macros.
  3. 3. Insert the macro: In the VBA editor, select Insert > Module and paste the BuildRandomRankArray code from the solution above. Adjust the three configuration variables if needed.
  4. 4. Run and save: Select the target worksheet, run BuildRandomRankArray from the macro controls, and save the workbook in a macro-enabled format such as XLSM.
Generate sequence, random-number, and rank columns in one macroOpen and edit Microsoft Excel XLSX and XLSM spreadsheet formatsWork in a familiar spreadsheet interfaceAdjust the row count, interval, and starting value in the code
microsoft office alternative - wps office

Frequently Asked Questions

How do I change the random-number range?

In the VBA module, replace Int(Rnd * 100) + 1 with Int(Rnd * (maxValue - minValue + 1)) + minValue, using your desired integer bounds.

Why do two rows have the same rank?

Rnd can generate duplicate integers. The macro gives equal random values the same rank and skips subsequent ranks, like a descending competition ranking.

How can I rank the smallest random number first?

In the ranking loop, change data(j, 2) > data(i, 2) to data(j, 2) < data(i, 2). The smallest value will then receive rank 1.

How do I keep the macro in my Excel workbook?

Choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). Enable macros only when you trust the workbook and its code.