Hi Johannes,
We can store the userform location before closing the form in the registry key. And load those values when we open the form. So it remembers where it was closed and opened there.
Option Explicit
Private Const AppName As String = "MyApplication"
Private Const StartX As String = "StartPositionX"
Private Const StartY As String = "StartPositionY"
Private Sub UserForm_Activate()
Dim formX
Dim formY
'If there is no registry value stored yet, then leave them blank
'so userform will be opened at center of the screen
formX = GetSetting(AppName, Me.Name, StartX, "")
formY = GetSetting(AppName, Me.Name, StartY, "")
If IsNumeric(formX) And IsNumeric(formY) Then
Me.Move formX, formY
End If
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Closing the form, store location information
SaveSetting AppName, Me.Name, StartX, Me.Left
SaveSetting AppName, Me.Name, StartY, Me.Top
End Sub
Since I used a form named as Userform1, the registry keys will be saved at this path:
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\MyApplication\UserForm1
You can set "MyApplication" however you like.
Is this method safe ? Yes, it is a method mostly we use for storing small data since it is very easy to retrieve and save by using GetSetting and SaveSetting function.
Attached is the sample workbook for this. I hope it helps.
Suat