Locate where my Form terminates

southfarm

New Member
Joined
Jan 13, 2017
Messages
5
Hi,

Hi I'm trying to find the VBA code for determine where I terminate my form so when I initiate it again it will be located at the same position. I want to save the position into the value of a cell. I've tried:

Private Sub UserForm_Terminate()
Range("xPos").Value = Me.Left
Range("yPos").Value = Me.Top
End Sub

But it only returns 0,0 (default in the form properties)

Thank you for any help!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi and welcome to the MrExcel Message Board,

Try this, instead:
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Range("xPos").Value = Me.Left
    Range("yPos").Value = Me.Top
End Sub
The problem with the Terminate event is that the UserForm has already "gone" by then so it has no position.


Regards,
 
Upvote 0
Ok. That makes sense. Is there any way one can collect the position every time it's getting moved to a new location? That would give me the same result!
 
Upvote 0
There is a solution posted elsewhere in this very forum!!!


Code:
[COLOR=#333333]Private Sub UserForm_Activate()    [/COLOR][INDENT]Me.Top = [yPos][/INDENT]
[INDENT]Me.Left = [xPos][/INDENT]
End Sub


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If Not [yPos] = Me.Top Or Not [xPos] = Me.Left Then
        [yPos] = Me.Top
        [xPos] = Me.Left        
    End If
[COLOR=#333333]End Sub
[/COLOR]

I modified it to match your named ranges. It might even work!

http://www.mrexcel.com/forum/excel-questions/671108-userform-startup-position.html
 
Upvote 0
I'm very very sorry, but I have been searching for 1-2 hours before I posted my question :( I'm just doing the best I can.
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,840
Members
449,193
Latest member
MikeVol

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