Random Position of Userform

ben.burgess

Board Regular
Joined
Sep 7, 2005
Messages
109
Hi guys,

I am trying to run a practical joke looping a userform 1000 times or so (more if I really want to gt mean) so that their screen fills with the boxes. Is there a way I can randomly position the user form rather than having it come up in the middle of the screen?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
http://cpearson.com/excel/FormPosition.htm

download his module and use his code

I'm not sure of any other methods

Chips utility is for positioning the form at a particular place with respect to a given cell. Too complicated for a simple joke.

First, in the VB Editor, on the Properties pane, change the StartUpPosition property of the UserForm to 0 - Manual. Then when opening the form, use something like this in the UserForm_Initialize procedure:

Code:
Private Sub UserForm_Initialize()
  Me.Top = Rnd * (Application.Height - Me.Height)
  Me.Left = Rnd * (Application.Width - Me.Width)
End Sub

The module that makes it fly is as simple as this:

Code:
Sub ThousandForms()
  Dim i As Long
  Const iMax As Long = 100
  Dim frm() As UserForm1
  ReDim frm(1 To iMax + 1)
  For i = 1 To iMax
    Set frm(i) = New UserForm1
    frm(i).Show vbModeless
  Next
  Set frm(iMax + 1) = New UserForm1
  frm(iMax + 1).Show vbModeless
End Sub
 
Upvote 0
This version closes all the forms when the top one is closed:

Code:
Sub ThousandForms()
  Dim i As Long
  Const iMax As Long = 100
  Dim frm() As UserForm1
  ReDim frm(1 To iMax + 1)
  For i = 1 To iMax
    Set frm(i) = New UserForm1
    frm(i).Show vbModeless
  Next
  Set frm(iMax + 1) = New UserForm1
  frm(iMax + 1).Show
  
  For i = iMax To 1 Step -1
    Unload frm(i)
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,604
Messages
6,160,748
Members
451,670
Latest member
Peaches000

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