# Macro so you can go to sleep



## Sunvisor (Feb 9, 2010)

Is there a macro I can create so I can get some sleep in front of my computer. I have my back turned to the office and I think as long as my hand is on a mouse and scrolling is being done in excel, no questions will be raised. =P


----------



## Smitty (Feb 9, 2010)

You're kidding right?


----------



## jbeaucaire (Feb 9, 2010)

Sorry, this made me laugh so hard I had to give it a shot!

This macro will be hidden, so it can only be activated by typing the name of the macro into the *Alt-F8* popup window.  Or you can assign it to a button, but again, you'll have to manually type *SleeperAgent* into the Assign Macro dialog.


```
Option Explicit

Private Sub SleeperAgent()
Dim cnt As Long, Timer As String, ws As Worksheet, MyArr

cnt = Application.InputBox("How many times through?", "Loop Count", 200)
If cnt = False Then Exit Sub
On Error GoTo 0
MyArr = Array("00:00:01", "00:00:02", "00:00:03", "00:00:04")

Do
    For Each ws In Worksheets
        ws.Activate
        Timer = Int((3 - 1 + 1) * Rnd + 1) - 1    'random number between 1 and 3.
        Application.Wait (Now + TimeValue(MyArr(Timer)))
        ActiveWindow.SmallScroll Down:=Timer
        ActiveWindow.SmallScroll ToRight:=Timer
        Application.Wait (Now + TimeValue(MyArr(Timer)))
        Range("A1").Activate
        Randomize
    Next ws
    
    cnt = cnt - 1
    
Loop Until cnt = 0

Beep

End Sub
```

Hehe...shameless, dude, really shameless.


----------



## sous2817 (Feb 9, 2010)

jbeaucaire said:


> Sorry, this made me laugh so hard I had to give it a shot!
> 
> This macro will be hidden, so it can only be activated by typing the name of the macro into the *Alt-F8* popup window.  Or you can assign it to a button, but again, you'll have to manually type *SleeperAgent* into the Assign Macro dialog.
> 
> ...



Hello jbeaucaire,

Seems to give me an error when I run it.  No message just a red circle w/ an X the number "400" with the option to "ok" or "help" in the VBA editor.


----------



## sous2817 (Feb 9, 2010)

Stepping through the code gives me a Run-time error '1004' on this line:


```
Range("A1").Activate
```

when it gets to the 2nd sheet.  Works fine on the first sheet.


----------



## jbeaucaire (Feb 9, 2010)

Hmm, odd. It cycles fine for me on Excel 2003.

NOTE: To break out of the sleep-scrolling, press ESC twice.


----------



## sous2817 (Feb 9, 2010)

I'm using 2007...


----------



## TinaP (Feb 9, 2010)

It works for me.  

Perhaps too well...


----------



## jbeaucaire (Feb 9, 2010)

This was purely for fun and I'm sure there enough in that macro you can construct one for yourself to do what you want it to do in a loop.

Have fun...ZZZZZZzzzzzzzzzzzzz


----------



## erik.van.geit (Feb 10, 2010)

sous2817 said:


> Stepping through the code gives me a Run-time error '1004' on this line:
> 
> 
> ```
> ...


 you must have your code put in a worksheetmodule
it tries to activate A1 on the sheet where the code resides while it is not active

solution:
put your code in a normal module or workbookmodule

kind regards,
Erik


----------



## Sunvisor (Feb 10, 2010)

A very impressive way to be lazy. This is great hah.

I knew this would be a fun experiment...


----------

