Placing a form with VBA

G

Gerrit Kiers

Hi all.

I'd like to place my form based on the screenresolution of the user.

I want to right align on the screen so use value based on screenwidth
minus formwidth.

I did find a solution to this based on the
Application.Width value.

But I cannot write a code for it, since the
Userform.Left value
needs to be significantly smaller than what I would calculate from
Application.Width - Userform.Width

What is the relation between these two widths? How can I calculate the
form position from these values?

Kind regards, Gerrit
 
J

jaf

Hi Gerrit,
To center on the visible window try this.

centerofapp=application.width/2
Userform.left=centerofapp-(Userform.Width/2)
 
G

Gerrit Kiers

Hi Gerrit,
To center on the visible window try this.

centerofapp=application.width/2
Userform.left=centerofapp-(Userform.Width/2)

John,

Thanks for your suggestion. If I am not mistaken the VBA IDE has an
option available in the properties window of the form for centering on
the screen.
You'll find a tag StartUpPosition and you'll be able to set it to :
Manual
Center Owner
Center Screen
Windows Default

Indeed do I look for a formula to calculate the position. I need to
convert, since for some vage reason the *unit* of VBA Width value of
Objects seems not to be pixel.

Try to make a form and make it as big as your screen (so 768 * 1024,
or 600 * 800) and you'll find your form to have become much bigger
than your screen. Ehat might be the clue here?

Thanks for your reply!

Kind regards, Gerrit
 
G

Gerrit Kiers

Hi Gerrit,
width is in points. (72/inch)


Thanks John, I solved it thanks to you. In Excel I do not need any
conversion. The next code places UserForm1 in the lower right corner
of the application. It takes windowstate in account and aligns neatly
independant of screenresolution

Case Select Application.WindowState
Case xlMaximized
UserForm1.Left = Application.Width - UserForm1.Width - 6
UserForm1.Top = Application.Height - UserForm1.Height - 6
Case Else
UserForm1.Left = Application.Width - UserForm1.Width - 4
UserForm1.Top = Application.Height - UserForm1.Height - 4
End Select


I was puzzled since I was also working on a VBA project in a non-MS
application. Here I do have to reduce the application width by 3/4.
This results in:

UserForm1.Left = (0.75 * Application.Width) - UserForm1.Width - 6
UserForm1.Top = (0.75 *Application.Height) - UserForm1.Height - 6

Gerrit
 
G

Gerrit Kiers

I get an "Expected: Expression" error on the first line of the code below.

My mistake! Swap 'Select' and 'Case'.
First line should be:

Select Case Application.WindowState

Gerrit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top