Excel Prank - Random Cell Selection

Add to Favorites
Author: | 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. :)

Sections:

The Code

Where to Install

Notes

The Code

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

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

5ef421be79a1306cb1fa3ef7ea162286.jpg

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.

d1e6859826e63c06a2b9313a78289521.jpg

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

Notes

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.


Downloadable Files: Excel File

Question? Ask it in our Excel Forum


Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons 50+ Hours of Instruction 200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Tutorial Details
Downloadable Files: Excel File
Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course