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.
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
Note: this goes into a Worksheet code section and not a Module, look to the next section for instructions.
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.