Excel Prank - Random Pop-up Window

Add to Favorites
Author: | Edits: don

Excel prank that makes a pop-up window display randomly after the user selects a cell.

This works such that the user could select 20 different cells with nothing happening or after 5 cell selections, the pop-up could appear.



The Code

Where to Install


The Code

Public counter As Integer
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

counter = counter + 1

maxCount = 15
minCount = 5

randNumber = Int((maxCount - minCount + 1) * Rnd() + 1)

If counter = randNumber Then

    MsgBox "Error! Excel hates you...", 48

    counter = 0

End If

If counter > maxCount Then

    counter = 0

End If

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; however, you still need the line that begins with Public to go at the top fo the code window.

The only way to stop this prank is to remove the code from the workbook or disable macros; that said, this macro only serves as a minor inconvenience.

Download the attached file to see this macro in action.

Downloadable Files: Excel File

Question? Ask it in our Excel Forum

Our Excel Courses

  • Skill Level: All Levels
  • |
  • 5 hours+

How to make a fully featured professional form in Excel that is unbreakable. This includes how to use the form to store, view, edit, and delete data from a data storage worksheet.

  • Skill Level: All Levels
  • |
  • 2 hours

Send Emails from Excel using VBA and Macros. This course starts from the Basics and builds up to more advanced examples with attaching workbooks, worksheets, PDF's, automatically sending emails, including a signature, error handling, increasing speed, and more.