Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 47
Default Save Form Position on Close


I have a userform that I've added to a spreadsheet that I've been using. It's
being used as a floating form. It can be moved anywhere on the form as the
user interacts with the spreadsheet. What I'd like to do is save the screen
position (top and left properties) when I close the form. So that the next
time the user opens the form it'll move back to the position it was in last
time.

So what I've done is I've coded the userform terminate event to write the
top and left properties to a sheet in the workbook. next time the form opens
it'll read those cells and move the form to the position saved.

Here's what I've got:

Private Sub UserForm_Terminate()

Sheets("Manning Config").Range("BU3").Value = Me.Left
Sheets("Manning Config").Range("BV3").Value = Me.Top

End Sub


The problem though is that it's writing zeros to the cells and not the
actual screen position where the form is sitting. Is the Me.Left and Me.Top
properties not the ones that I should be reading from?

Also, I haven't yet coded the form activate event yet. I'll have to have
some condition that will get the screen width and height and make sure that
the saved top and left values aren't larger than the available screen space
so that it doesn't try to open the form in a place where the user can't get
it. I think I already know how to do this part though.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Save Form Position on Close


Try using:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)



Jonathan Brown wrote:

I have a userform that I've added to a spreadsheet that I've been using. It's
being used as a floating form. It can be moved anywhere on the form as the
user interacts with the spreadsheet. What I'd like to do is save the screen
position (top and left properties) when I close the form. So that the next
time the user opens the form it'll move back to the position it was in last
time.

So what I've done is I've coded the userform terminate event to write the
top and left properties to a sheet in the workbook. next time the form opens
it'll read those cells and move the form to the position saved.

Here's what I've got:

Private Sub UserForm_Terminate()

Sheets("Manning Config").Range("BU3").Value = Me.Left
Sheets("Manning Config").Range("BV3").Value = Me.Top

End Sub

The problem though is that it's writing zeros to the cells and not the
actual screen position where the form is sitting. Is the Me.Left and Me.Top
properties not the ones that I should be reading from?

Also, I haven't yet coded the form activate event yet. I'll have to have
some condition that will get the screen width and height and make sure that
the saved top and left values aren't larger than the available screen space
so that it doesn't try to open the form in a place where the user can't get
it. I think I already know how to do this part though.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Save Form Position on Close


ps. If you step through your code (for the _terminate event), you can go back
to excel and see that the userform has disappeared before the first line of code
is executed.

Maybe that's why you see 0's in those cells.

Jonathan Brown wrote:

I have a userform that I've added to a spreadsheet that I've been using. It's
being used as a floating form. It can be moved anywhere on the form as the
user interacts with the spreadsheet. What I'd like to do is save the screen
position (top and left properties) when I close the form. So that the next
time the user opens the form it'll move back to the position it was in last
time.

So what I've done is I've coded the userform terminate event to write the
top and left properties to a sheet in the workbook. next time the form opens
it'll read those cells and move the form to the position saved.

Here's what I've got:

Private Sub UserForm_Terminate()

Sheets("Manning Config").Range("BU3").Value = Me.Left
Sheets("Manning Config").Range("BV3").Value = Me.Top

End Sub

The problem though is that it's writing zeros to the cells and not the
actual screen position where the form is sitting. Is the Me.Left and Me.Top
properties not the ones that I should be reading from?

Also, I haven't yet coded the form activate event yet. I'll have to have
some condition that will get the screen width and height and make sure that
the saved top and left values aren't larger than the available screen space
so that it doesn't try to open the form in a place where the user can't get
it. I think I already know how to do this part though.


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Save Form Position on Close

Hi Jonathan,

By the time I worked out how to do this I see that Dave has already provided
what is probably a solution but thought I would post what I have discovered
anyway. You can use UserForm_Layout event to save the parameters.

To reset the form to the last used position you need to set the forms
StartupPosition property to 0 - Manual and then the code in the Initialize
event as I have written it.

For some reason I was not able to code the Move command using the named
parameters of Left:=, Top:= etc because I kept getting an error but simply
separating the values with a comma and placing them in the correct default
order it works. Perhaps someone can comment on this.


Private Sub UserForm_Layout()
'Saves the forms position, height and width.
Sheets("Sheet1").Range("A1") = Me.Left
Sheets("Sheet1").Range("A2") = Me.Top
Sheets("Sheet1").Range("A3") = Me.Width
Sheets("Sheet1").Range("A4") = Me.Height
End Sub

Private Sub UserForm_Initialize()
'NOTE: Set form property StartupPosition
'to 0 - Manual
'Order of parameters in code is
'Left, Top, Width, Height

UserForm1.Move _
Sheets("Sheet1").Range("A1"), _
Sheets("Sheet1").Range("A2"), _
Sheets("Sheet1").Range("A3"), _
Sheets("Sheet1").Range("A4")

End Sub


Regards,

OssieMac
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 47
Default Save Form Position on Close

Thanks Dave! That was the answer that I needed. It works perfectly now.

"Dave Peterson" wrote:

Try using:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)



Jonathan Brown wrote:

I have a userform that I've added to a spreadsheet that I've been using. It's
being used as a floating form. It can be moved anywhere on the form as the
user interacts with the spreadsheet. What I'd like to do is save the screen
position (top and left properties) when I close the form. So that the next
time the user opens the form it'll move back to the position it was in last
time.

So what I've done is I've coded the userform terminate event to write the
top and left properties to a sheet in the workbook. next time the form opens
it'll read those cells and move the form to the position saved.

Here's what I've got:

Private Sub UserForm_Terminate()

Sheets("Manning Config").Range("BU3").Value = Me.Left
Sheets("Manning Config").Range("BV3").Value = Me.Top

End Sub

The problem though is that it's writing zeros to the cells and not the
actual screen position where the form is sitting. Is the Me.Left and Me.Top
properties not the ones that I should be reading from?

Also, I haven't yet coded the form activate event yet. I'll have to have
some condition that will get the screen width and height and make sure that
the saved top and left values aren't larger than the available screen space
so that it doesn't try to open the form in a place where the user can't get
it. I think I already know how to do this part though.


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form Startup Position kirkm[_8_] Excel Programming 3 May 30th 09 05:36 PM
Form Position Steven Excel Programming 5 December 15th 08 03:06 PM
Close a form when you click outside of the form Alex St-Pierre Excel Programming 1 June 19th 08 10:57 PM
Call user form from ThisWorkbook; close file if form closed XP Excel Programming 2 July 20th 07 07:04 PM
Form start position drewdog Excel Programming 2 April 6th 04 02:15 AM


All times are GMT +1. The time now is 06:24 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"