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.

baeb1a6541a0f9ec56cc929b637eeae5.jpg

Sections:

The Code

Where to Install

Notes

The Code

Public counter As Integer
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'TeachExcel.com

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

92df3b332f3cd3eeb290feae4eaac08e.png

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.

ba77bfd58c99efac23602d7e3323d63a.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; 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


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