Excel Prank - Zooming into Craziness - For Fun :)

Add to Favorites
Author: | Edits: don

This Excel Prank will randomly zoom in or out when the user selects a cell - after the initial zoom, if the user selects another cell, it will go back to its previous normal zoom setting.

In this tutorial I will show you how to use the Excel prank macro that randomly resizes the worksheet, but I will also show you how every line of code within the macro works.

If you just want to get the code, you are covered and if you also want to learn how the code works, you are also covered!

The macro in this prank is a great base for testing code and features in VBA because it combines many topics, including:

  • IF statements in VBA.
  • Worksheet function usage inside of a macro.
  • Static variables that live beyond the life of the macro.
  • True/False Booelan variables.
  • Events in Excel.

Zoom Prank Code

Where to Install: Inside the worksheet code module where you want it to run.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' TeachExcel.com
' Sheet Zooming Prank
' This macro will zoom the sheet in or out at random intervals.
' - It runs each time the user selects a new cell, but zooms the sheet randomly,
'   not every time.
' - After a sheet has been resized, the next time a cell is selected, it will be
'   reset back to the original size.
' Static variables that live until Excel is closed or the project reset.
' - These let us know if the worksheet was previously zoomed by us and what
'   the original zoom value was so we can reset it.
Static isZoomed As Boolean
Static zoomLast As Long
' Random number value variable and threshold.
Dim randNum As Double
Dim randNumZoomThreshold As Double
' Zoom value variables.
Dim zoomMax As Long
Dim zoomMin As Long
Dim zoomNew As Long
' Max and min zoom that you want to set.
' - Set these to any limit that you want inside of 400 and 10.
' - Max possible is 400% zoom and min is 10% zoom.
zoomMax = 400
zoomMin = 10
' Generate a new zoom level between 10 and 400.
' - Use a worksheet function because there is no RandBetween in VBA and this is easy.
zoomNew = Application.WorksheetFunction.RandBetween(zoomMin, zoomMax)
' Get a random number.
' - This will return a decimal from 0-1
randNum = Rnd()
' Zoom threshold.
' - The closer to 0, the more the sheet will be zoomed and the closer to 1, the less
'   it will be zoomed.
' - This will be compared against the result of the Rnd() function in order to
'   determine when to zoom the worksheet.
randNumZoomThreshold = 0.75
' Check if we should zoom the sheet or not.
If isZoomed Then
    ' If here, we zoomed it last time, so un-zoom it this time.
    ' Set the zoom.
    ActiveWindow.Zoom = zoomLast
    ' Store that the zoom has been reset.
    isZoomed = False
ElseIf randNum > randNumZoomThreshold Then
    ' If here, we want to zoom the sheet.
    ' Store the current zoom value so we can reset it to this later.
    zoomLast = ActiveWindow.Zoom
    ' Set the zoom.
    ActiveWindow.Zoom = zoomNew
    ' Store that the zoom has been set.
    isZoomed = True
End If
End Sub

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.