Matrix Digital Rain in Excel

Falling green code, like the film The Matrix, made with RANDBETWEEN, three conditional formatting rules and a tiny macro. A fun way to learn how MOD, ROW and conditional formatting work together.

FunctionsRANDBETWEEN, MOD, ROW
Formatting3 conditional formatting rules
Macro8 lines, no Windows API

Matrix digital rain animation in Excel

The idea comes from Nitin Mehta of XLnControl (Engineers-Excel.com); I’ve added a macro to run it.

Step 1: Random digits

  1. Select B1:AH40, type =RANDBETWEEN(0,9) and press Ctrl + Enter.
  2. Give the range a black fill and black font, so nothing shows yet.
  3. Narrow the columns to about 2.5 and set a monospaced font such as Consolas.
RANDBETWEEN formula in a cell
Cells with black fill and black font

Step 2: Three conditional formatting rules

With B1:AH40 still selected, add each rule with Home → Conditional Formatting → New Rule → Use a formula. A1 will hold a counter that the macro increases.

Rule Formula Font colour
Head of each stream =MOD($A$1,15)=MOD(ROW()+B$1,15) White
Just behind it =MOD($A$1,15)=MOD(ROW()+B$1+1,15) Bright green
The tail =OR(MOD($A$1,15)=MOD(ROW()+B$1+2,15),MOD($A$1,15)=MOD(ROW()+B$1+3,15),MOD($A$1,15)=MOD(ROW()+B$1+4,15),MOD($A$1,15)=MOD(ROW()+B$1+5,15)) Dark green
First conditional formatting rule
Second rule for bright green
Third rule for the dark green tail

How the formulas work

Each column’s row-1 value is a random offset, so each stream starts at a different height. A cell lights up when MOD(A1,15), the counter, equals MOD(row + offset, 15). As A1 goes up by one, the matching row moves down by one: the code falls.

Worked example with A1 = 13: in J8, where J1 is 5, MOD(13,15)=MOD(8+5,15) gives 13 = 13, TRUE, so J8 is white. In J7, MOD(7+5+1,15) is 13, so J7 is bright green.

Finally, copy row 1 and paste it as values, so the offsets stay fixed while the digits below keep changing.

Grid after applying conditional formatting

Step 3: The macro

Alt + F11 → Insert → Module → paste:

Sub DigitalRain()
    Dim i As Long, t As Single
    For i = 0 To 100
        Range("A1").Value = i
        t = Timer
        Do While Timer < t + 0.1: DoEvents: Loop
    Next i
End Sub

Each step changes A1, which recalculates RANDBETWEEN (new digits) and the formatting (the streams move). The Timer loop waits a tenth of a second. Unlike the original Sleep version, it works in 32- and 64-bit Excel and on Mac.

Insert a button (Developer → Insert → Button), assign DigitalRain, and click it.

Digital rain running from a button

Related