Good Excel Practical Jokes, Pranks, Mean Tricks, etc.

Thought I would bring this thread back with a new one I thought of this morning.

This code when run will start randomly moving the cells around (at least it will LOOK like it is).
It works by randomly choosing two cells in the activewindow creating a images of those cells and placing them over the cells and then swapping their locations.

Call GoCrazy to start the macro.
Press F12 to exit the loop.

If you use CTRL+BREAK to exit, you will need to manually run the CureCrazy Sub which removes all created shapes.

You can set the Worksheet_Activate event to call GoCrazy and then when a user goes to the specified sheet, the craziness will begin.
NOTE: This works BEST on a sheet with lots of data and variations to colors, etc. just because you can see everything moving around easily.
It is actually pretty disconcerting to watch even though I know there is a simple UNDO to Cure the Crazy. :LOL:

Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Const VK_F12 = &H7B
Private CRAZY As Boolean
Sub GoCrazy()
Dim Lo_C As Long, Hi_C As Long
Dim Lo_R As Long, Hi_R As Long
Dim c1 As Range, c2 As Range
Dim Shp1 As Shape, Shp2 As Shape
Dim tmpLeft As Long, tmpTop As Long, tmpWidth As Long, tmpHeight As Long
Dim shpCount As Long
CRAZY = True

    Application.OnKey "{F12}", ""
    Do While CRAZY
        Lo_C = ActiveWindow.VisibleRange.Resize(1, 1).Column
        Hi_C = ActiveWindow.VisibleRange.Columns.Count + Lo_C - 1
        Lo_R = ActiveWindow.VisibleRange.Resize(1, 1).Row
        Hi_R = ActiveWindow.VisibleRange.Rows.Count + Lo_R - 1
        col1 = Int((Hi_C - Lo_C + 1) * Rnd + Lo_C)
        col2 = Int((Hi_C - Lo_C + 1) * Rnd + Lo_C)
        row1 = Int((Hi_R - Lo_R + 1) * Rnd + Lo_R)
        row2 = Int((Hi_R - Lo_R + 1) * Rnd + Lo_R)
        Set c1 = ActiveWindow.ActiveSheet.Cells(row1, col1)
        Set c2 = ActiveWindow.ActiveSheet.Cells(row2, col2)
        Set Shp1 = GetShape(c1)
        Set Shp2 = GetShape(c2)
        
        If Shp1 Is Nothing Then
            Set Shp1 = CreateCrazy(c1, shpCount)
            shpCount = shpCount + 1
        End If
        
        If Shp2 Is Nothing Then
            Set Shp2 = CreateCrazy(c2, shpCount)
            shpCount = shpCount + 1
        End If
    
        tmpLeft = Shp1.Left
        tmpTop = Shp1.Top
        tmpWidth = Shp1.Width
        tmpHeight = Shp1.Height
        Shp1.Left = Shp2.Left
        Shp1.Top = Shp2.Top
        Shp1.Width = Shp2.Width
        Shp1.Height = Shp2.Height
        Shp2.Left = tmpLeft
        Shp2.Top = tmpTop
        Shp2.Width = tmpWidth
        Shp2.Height = tmpHeight
        
        DoEvents
        If GetAsyncKeyState(VK_F12) Then StopCrazy
        DoEvents
    Loop
    Application.OnKey "{F12}"
End Sub
Sub StopCrazy()
    CRAZY = False
    CureCrazy
End Sub
Function CreateCrazy(Cll As Range, num As Long) As Shape
Dim newShape As Shape
Set currSelect = Selection
    Application.ScreenUpdating = False
        Cll.CopyPicture
        ActiveWindow.ActiveSheet.Paste Cll
        Set newShape = GetShape(Cll)
        newShape.Name = "CrazyShp" & num
        newShape.Fill.Visible = msoTrue
        newShape.Line.Visible = msoFalse
        
        DoEvents
    currSelect.Select
    Application.ScreenUpdating = True
    Set CreateCrazy = newShape
End Function
Private Function GetShape(rngSelect As Range) As Shape
Dim Shp As Shape
    
    For Each Shp In rngSelect.Worksheet.Shapes
        If Not Intersect(Range(Shp.TopLeftCell, Shp.BottomRightCell), rngSelect) Is Nothing Then
            GoTo shapeFound
        End If
    Next
    
    Set GetShape = Nothing
    Exit Function
shapeFound:
    Set GetShape = Shp
End Function

Sub CureCrazy()
Dim Shp As Shape
    For Each Shp In ActiveWindow.ActiveSheet.Shapes
        If Shp.Name Like "CrazyShp*" Then Shp.Delete
    Next Shp
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi BiocideJ,
This is cool bro thx for sharing, the best part is that now u gave me the best way to stop a loop, I've been looking through this forum and other places for an answer no answer was as good as this one, now I can stop the loops very simply, I rlly appreciate BiocideJ.
And whoever will say that there are other ways to stop a loop i know 'em but I liked this one XD
ZAX
 
Hey BiocideJ,

I love this code. I like how it looks like your computer is losing its mind but everything can be immediately undone. Very cool!
 
Hey BiocideJ,

I love this code. I like how it looks like your computer is losing its mind but everything can be immediately undone. Very cool!

and at first I thought it was highlighting different cells but then read the code and realized.
ZAX
 
I had a minor complaint/Comparison with another dude that my sheets weren't very colourful, and his were. So I wrote this:

Code:
Sub RandomColours()
Dim iR As Integer
Dim iG As Integer
Dim iB As Integer
Dim LastRow As Integer
Dim LastCol As Integer
Dim WholePage As Range
Dim cCell As Range
Application.ScreenUpdating = False
LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
LastCol = Cells.SpecialCells(xlCellTypeLastCell).Column
Set WholePage = Range(Cells(1, 1), Cells(LastRow, LastCol))
For Each cCell In WholePage
    iR = Rnd() * 255
    iG = Rnd() * 255
    iB = Rnd() * 255
    Debug.Print "Row = " & cCell.Row & "; Col = " & cCell.Column & "; Colour = " & RGB(iR, iG, iB)
    cCell.Font.Color = RGB(iR, iG, iB)
Next

Set WholePage = Nothing
Application.ScreenUpdating = True
End Sub
 
That is great.

And if you REALLY want to taste the rainbow,

add a fill color too. :eek:
Code:
cCell.Font.Color = RGB(iR, iG, iB)
cCell.Interior.Color = RGB(255 - iR, 255 - iG, 255 - iB)
 
That is great.

And if you REALLY want to taste the rainbow,

add a fill color too. :eek:
Code:
cCell.Font.Color = RGB(iR, iG, iB)
cCell.Interior.Color = RGB(255 - iR, 255 - iG, 255 - iB)
I like the idea of the fill color, but doing it presents two problems... one, sometimes the text is unreadable because the font and fill colors are the same or nearly the same; and, second, the font colors do not always "stand out" as a color because they are overwhelmed by all the color around them. My solution is to only randomize the fill color and set the font color to either white or black depending on which stands out better against the fill color.

Code:
Sub RandomColours()
  Dim iR As Long
  Dim iG As Long
  Dim iB As Long
  Dim LastRow As Long
  Dim LastCol As Long
  Dim Luminance As Long
  Dim BackColor As Long
  Dim WholePage As Range
  Dim cCell As Range
  Application.ScreenUpdating = False
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  LastCol = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
  Set WholePage = Range(Cells(1, 1), Cells(LastRow, LastCol))
  For Each cCell In WholePage
      iR = Rnd() * 255
      iG = Rnd() * 255
      iB = Rnd() * 255
      BackColor = RGB(255 - iR, 255 - iG, 255 - iB)
      cCell.Interior.Color = BackColor
      cCell.Font.Color = -vbWhite * (77 * (BackColor Mod &H100) + _
                         151 * ((BackColor \ &H100) Mod &H100) + 28 * _
                         ((BackColor \ &H10000) Mod &H100) < 32640)
  Next
  
  Set WholePage = Nothing
  Application.ScreenUpdating = True
End Sub

Note: I also changed all the Integer declarations to Long and change how the LastRow and LastCol are calculated.
 
Last edited:
Code:
cCell.Font.Color = -vbWhite * (77 * (BackColor Mod &H100) + _
                         151 * ((BackColor \ &H100) Mod &H100) + 28 * _
                         ((BackColor \ &H10000) Mod &H100) < 32640)


Clever.

This, coupled with my previous 'prank' makes for a very interesting effect too. Hehehe
 
Nice work Gents. I like that I can pique the interest of others...

I also like the method of finding Last row/column.

Code:
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  LastCol = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column

I don't think I've seen that before.
 

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top