VBA Userform Position

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: VBA Userform Position

Post by LisaGreen »

Hans,

Any idea why sometimes Left doesn't alter and sometimes it does??

Lisa

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Userform Position

Post by HansV »

I think it has to do with the position of the VBE window, in particular whether the VBE is maximized or not.
Best wishes,
Hans

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: VBA Userform Position

Post by LisaGreen »

Still trying things... Am "plotting" in Excel, the numbers for TOP after setting them in Initialize and how they come up in Layout. Running Initialize, and stopping.

I'm getting completely different numbers depending on which I set first Top or Left!

Can you confirm that at all Hans??

Lisa

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: VBA Userform Position

Post by LisaGreen »

Hi... me again... I've started a thread about this on the codecage along with an attachement of an excel file with one userform in.

Just to let everyone know of the cross posting.

Lisa

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Userform Position

Post by HansV »

It looks like userforms and the VBE are using different coordinate systems, and the Top, Left, Height and Width properties mix these coordinate systems in strange ways.
Let us know what you learn from the Code Cage forum (VBE Userform position)
Best wishes,
Hans

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: VBA Userform Position

Post by Jan Karel Pieterse »

Pragmatic approach...

I ran a loop that changed the VBE position and then showed the form repeatedly using various left and top values (in that order: first set left, then set top).

From this I found, that you can recalculate the .left and .top values you have to set from the actual values last saved.

Like so:

UserForm1.Left = (dCurLeft-22.5) - Application.VBE.MainWindow.Left * 0.75

Ditto for the top of the form:

UserForm1.Top = (dCurTop-12) - Application.VBE.MainWindow.Top * 1.5
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Userform Position

Post by HansV »

That corresponds to what I found in Post=85111 earlier in this thread:

Code: Select all

    ' ***** Adjust position of form *****
    Me.Top = ilTop - 1.5 * Application.VBE.MainWindow.Top - 45
    Me.Left = ilLeft - 0.75 * Application.VBE.MainWindow.Left - 6
The difference in the numbers is caused partially by the order - I set Top first, then Left, you set Left first, then Top. But it may also have to do with the DPI setting in Windows.
Best wishes,
Hans

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: VBA Userform Position

Post by Jan Karel Pieterse »

Good to know. At least we now have two systems that seem to behave the same.
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: VBA Userform Position

Post by LisaGreen »

Hi HAnsV and Jan Karel,

LOL!! I put the code in that you both advised and now the form... on my lappy anyway... edges down and off the screen to the left!!

I'm attaching the file.

Hugs... Lisa
You do not have the required permissions to view the files attached to this post.

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: VBA Userform Position

Post by LisaGreen »

Hey guys,

This is from the codecage....
http://www.thecodecage.com/forumz/micro ... -post.html" onclick="window.open(this.href);return false;

For me it's stable after the first button click if you use a literal value for top and left, so if your activate event calls the button_click code, it seems to be static thereafter. It seems as though the initial top and left are relative to the top left of the whole VBE but after that it's relative to the main window below the command bars.
__________________
Rory
Microsoft MVP - Excel

Lisa

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Userform Position

Post by HansV »

I can't view the post on the Code Cage, since I'm not a member there.
In the workbook that you posted in your previous reply, the userform moves, but if I change the code to what I posted earlier:

Code: Select all

    Me.Top = ilTop - 1.5 * Application.VBE.MainWindow.Top - 45
    Me.Left = ilLeft - 0.75 * Application.VBE.MainWindow.Left - 6
the position remains stable. You should declare ilTop and ilLeft as Single instead of Integer, by the way.
Best wishes,
Hans

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: VBA Userform Position

Post by LisaGreen »

Hey Hans,

Why single???

Lisa

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Userform Position

Post by HansV »

If you look at the Properties window, you'll notice that theTop, Left, Height and Width values can contain decimal places, e.g. 16.5 or 87.25. The Integer data type can only store whole numbers, so if you force the coordinates to be Integers, you may get tiny differences with the original position.
Best wishes,
Hans

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: VBA Userform Position

Post by LisaGreen »

I really should have thought of that....What a stupid woman!! Thanks...

Lisa

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: VBA Userform Position

Post by LisaGreen »

Heya... Just to round this off... I save the final position in an INI file in the Terminate event. Then I read that back on reopening the form and try to set the form position back where it was. Note the word "try".

Guess what... the form was moving down and to the right a bit.

Suddenly I remembered how you got the numbers you did!! hehehehe :-)

In Initialize....

Code: Select all

slInitialState = UCase$(fncReadINIValue("options", "InitialState", False))
silTop = CSng(fncReadINIValue("options", "Top", False))
silLeft = CSng(fncReadINIValue("options", "Left", False))

silLeft = silLeft - 6
silTop = silTop - 45

' Want slInitialState so set to...
Me.StartUpPosition = 0
Me.Top = silTop
Me.Left = silLeft
Left - 6
Top - 45

Does the trick here at the moment. Only tried it with all windows maxed at the mo.

Lisa

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: VBA Userform Position

Post by LisaGreen »

Hi guys,

This is happening again. I have a different laptop now though and I've changed the size of the window... so stupid me can't tell if it's the laptop or not!! :groan: :sad:

I tried saving the position in module variables in the cmd button enter event but it doesn't appear to be firing!!

After a bit of playing... changing...
Me.Top = ilTop - 1.5 * Application.VBE.MainWindow.Top - 45
Me.Left = ilLeft - 0.75 * Application.VBE.MainWindow.Left - 6

to ...
Me.Top = ilTop - 1.5 * Application.VBE.MainWindow.Top - 29
Me.Left = ilLeft - 0.75 * Application.VBE.MainWindow.Left - 6

... has worked as a workaround.

Any further thoughts? I know it's a couple of years back and appreciate that you guys are busy so don't break an ankle or anything. I may just have to leave it as a "known problem".

Hugs
Lisa

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Userform Position

Post by HansV »

The values I found earlier were established by trial and error, I don't have a satisfactory explanation. Changes in screen resolution/pixel density may affect the values...
Best wishes,
Hans