Excel Prank - Random Cell Selection

Add to Favorites
Author: don | Edits: don

Excel prank where a random cell is selected each time a user tries to select a cell.

For instance, if you select cell A1, Excel will autmotically select another cell tthat is near A1. Each time a cell is selected, even if it is the same cell over and over, a new random cell will be selected automatically.

This prank does require that the user has macros enabled. Also, it will annoy them really quickly, so be nice and stay nearby so you can fix it for them. :)


The Code

Where to Install


The Code

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Select a random cell after a user selects any cell.

Application.EnableEvents = False

curRow = Target.Row
curCol = Target.Column

posNegZero = CInt((Rnd() * ((-1) ^ Int(Rnd() * 10))) * 1)

'Max number of rows to offset by.
offsetValueRow = 3
'Max number of columns to offset by.
offsetValueCol = 3

RandomOffsetRow = Int((offsetValueRow - 1 + 1) * Rnd() + 1)
RandomOffsetCol = Int((offsetValueCol - 1 + 1) * Rnd() + 1)

If RandomOffsetRow >= curRow Or RandomOffsetCol >= curCol Then
    posNegZero = -1
End If

RandomOffsetRow = RandomOffsetRow * posNegZero
RandomOffsetCol = RandomOffsetCol * posNegZero

'Select the random cell.
Cells(curRow - RandomOffsetRow, curCol - RandomOffsetCol).Select

Application.EnableEvents = True

End Sub


Note: this goes into a Worksheet code section and not a Module, look to the next section for instructions.

Where to Install

Worksheet code section.

Alt + F11 (VBA window) > look to the Project window on the left side of the window (Ctrl + R if you don't already see it) > double-click the name of the worksheet where you want this prank to work > put the code into the window that opens.


You must put this code into the code section for each worksheet where you want it to work.


This macro will only work on the worksheet or worksheets where you insert the code in the VBA window; you can't just apply it to the entire workbook.

Also, the macro works in the SelectionChange event; if you already have code in that event, then just copy/paste the above code without the lines that begin with Sub and End Sub.

The only way to stop this prank is to remove the code from the workbook or disable macros.

Download the attached file to see this macro in action.

Question? Ask it in our Excel Forum

Downloadable Files: Excel File