VBA Userform Position
-
- 5StarLounger
- Posts: 964
- Joined: 08 Nov 2012, 17:54
Re: VBA Userform Position
Hans,
Any idea why sometimes Left doesn't alter and sometimes it does??
Lisa
Any idea why sometimes Left doesn't alter and sometimes it does??
Lisa
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: VBA Userform Position
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
Hans
-
- 5StarLounger
- Posts: 964
- Joined: 08 Nov 2012, 17:54
Re: VBA Userform Position
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
I'm getting completely different numbers depending on which I set first Top or Left!
Can you confirm that at all Hans??
Lisa
-
- 5StarLounger
- Posts: 964
- Joined: 08 Nov 2012, 17:54
Re: VBA Userform Position
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
Just to let everyone know of the cross posting.
Lisa
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: VBA Userform Position
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)
Let us know what you learn from the Code Cage forum (VBE Userform position)
Best wishes,
Hans
Hans
-
- Microsoft MVP
- Posts: 656
- Joined: 24 Jan 2010, 17:51
- Status: Microsoft MVP
- Location: Weert, The Netherlands
Re: VBA Userform Position
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
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
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: VBA Userform Position
That corresponds to what I found in Post=85111 earlier in this thread:
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.
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
Best wishes,
Hans
Hans
-
- Microsoft MVP
- Posts: 656
- Joined: 24 Jan 2010, 17:51
- Status: Microsoft MVP
- Location: Weert, The Netherlands
Re: VBA Userform Position
Good to know. At least we now have two systems that seem to behave the same.
-
- 5StarLounger
- Posts: 964
- Joined: 08 Nov 2012, 17:54
Re: VBA Userform Position
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
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.
-
- 5StarLounger
- Posts: 964
- Joined: 08 Nov 2012, 17:54
Re: VBA Userform Position
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
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
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: VBA Userform Position
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:
the position remains stable. You should declare ilTop and ilLeft as Single instead of Integer, by the way.
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
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 964
- Joined: 08 Nov 2012, 17:54
Re: VBA Userform Position
Hey Hans,
Why single???
Lisa
Why single???
Lisa
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: VBA Userform Position
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
Hans
-
- 5StarLounger
- Posts: 964
- Joined: 08 Nov 2012, 17:54
Re: VBA Userform Position
I really should have thought of that....What a stupid woman!! Thanks...
Lisa
Lisa
-
- 5StarLounger
- Posts: 964
- Joined: 08 Nov 2012, 17:54
Re: VBA Userform Position
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....
Left - 6
Top - 45
Does the trick here at the moment. Only tried it with all windows maxed at the mo.
Lisa
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
Top - 45
Does the trick here at the moment. Only tried it with all windows maxed at the mo.
Lisa
-
- 5StarLounger
- Posts: 964
- Joined: 08 Nov 2012, 17:54
Re: VBA Userform Position
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!!
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
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!!
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
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: VBA Userform Position
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
Hans