Locking a userform position

elgringo56

Well-known Member
Joined
Apr 15, 2002
Messages
869
I have a userform that I would like to lock in position. As it is now, if the user "grabs" the top blue bar on the userform, he can move it around, and I would like to prevent that. Is this possible?
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,224
There might be some API calls out there in the universe, whereabouts unkown to us mortals, that support the disallowance of dragging a window (in your case, the userform) to a different location on the screen. In the meantime, have a look at Stephen Bullen's web site, and especially this page
http://www.bmsltd.co.uk/Excel/Default.htm
where his FormFun.zip download gives an example of a userform without the title bar, which would at least achieve the effect of having no way to move the userform, though you also lose the min, max, and close buttons that come with the title bar. Anyway, have a look at that and see if it helps.
 

Juan Pablo González

MrExcel MVP
Joined
Feb 8, 2002
Messages
11,959
This seems to work ok:

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Type</SPAN> Position
    Left <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>
    Top <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Type</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Layout()
    <SPAN style="color:#00007F">Static</SPAN> Pos <SPAN style="color:#00007F">As</SPAN> Position

    <SPAN style="color:#00007F">Dim</SPAN> Mvd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>
    
    <SPAN style="color:#007F00">'If the form is just being initialized, store the position</SPAN>
    <SPAN style="color:#00007F">If</SPAN> Pos.Left = 0 <SPAN style="color:#00007F">Or</SPAN> Pos.Top = 0 <SPAN style="color:#00007F">Then</SPAN>
        Pos.Left = Me.Left
        Pos.Top = Me.Top
        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    
    <SPAN style="color:#007F00">'Check to see if the form has been moved</SPAN>
    Mvd = <SPAN style="color:#00007F">False</SPAN>
    <SPAN style="color:#00007F">If</SPAN> Me.Left <> Pos.Left <SPAN style="color:#00007F">Then</SPAN>
        Me.Left = Pos.Left
        Mvd = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">If</SPAN> Me.Top <> Pos.Top <SPAN style="color:#00007F">Then</SPAN>
        Me.Top = Pos.Top
        Mvd = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    
    <SPAN style="color:#00007F">If</SPAN> Mvd <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "Please don't move me !", vbCritical
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209
There are a number of ways .... here is the API version
just for completeness :)

Code:
Option Explicit
'////////////////////////////////////////////////////////////
'// What you need to do is to Disable the System Menu itime
'// for the Move control.
'// The system menu or Control menu is the Dropdown list you
'// get when you Right click on the windows Top area or write
'// to Area that usually has the Window caption, in this case
'// the userforms Name caption. Std windows has six, Userforms
'// have 2 = Move & Close.
'// Intersetingly enought to disable the close button all you
'// need to add to this routine to Disable Move is to Loop
'// until you hit the close = 7 ? and NOT 6, this I believe
'// takes into account the instance where the Sytem menu has
'// a "Whats this button"
'////////////////////////////////////////////////////////////

Private Declare Function GetSystemMenu _
    Lib "user32" ( _
    ByVal hWnd As Long, _
    ByVal bRevert As Long) _
As Long

Private Declare Function RemoveMenu _
    Lib "user32" ( _
    ByVal hMenu As Long, _
    ByVal nPosition As Long, _
    ByVal wFlags As Long) _
As Long
    
Private Declare Function FindWindowA _
    Lib "user32" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) _
As Long
  
Private Const MF_BYPOSITION As Long = &H400

Private Sub UserForm_Initialize()
Dim lFrmHdl As Long, iCount As Integer
'// Ivan F Moala
lFrmHdl = FindWindowA(vbNullString, Me.Caption)

If lFrmHdl <> 0 Then
    '// MF_BYCOMMAND
    '//Indicates that uPosition gives the identifier of the menu item.
    
    '//If neither the MF_BYCOMMAND nor MF_BYPOSITION flag is specified,
    '//the MF_BYCOMMAND flag is the default flag.
    
    '// MF_BYPOSITION
    '//Indicates that uPosition gives the zero-based relative position of the menu item.
    '// ie 0,1,2,3 etc
    'Exit Sub
    '//Typical Windows has 6 menus
    '//Restore, Move, Size, Minimise, Maximize, Close
    '//Even though a Userform displays 2 = Move & Close
    '//By default Move is the Next item
    '//so just loop twice
    For iCount = 0 To 1
        RemoveMenu GetSystemMenu(lFrmHdl, False), 0, MF_BYPOSITION
    Next iCount
End If

End Sub
 

elgringo56

Well-known Member
Joined
Apr 15, 2002
Messages
869

ADVERTISEMENT

Ivan, that works perfect for keeping the UserForm locked in place, now, is there also a way, with it, to eliminate the close button? I was doing that with a HideCloseButton Me in UserForm_Initialize, but if I do that now, your routine will not work. There is so much here I dont understand.
 

elgringo56

Well-known Member
Joined
Apr 15, 2002
Messages
869
I tried doing a loop count 0 to 5, and that does clear the close button, but it also unlocks the userform. Almost as if one of those flags has to be set. Hmmmmmm.......
 

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,494
Hi,

Put this code in the Userform code module to disable the close button:

Code:
Private Sub UserForm_QueryClose(Cancel as integer,CloseMode as Integer)
If CloseMode = vbFormControlMenu Then
    MsgBox "Click OK to close this form"
    Cancel = True
End If
End Sub

HTH

alan
 

elgringo56

Well-known Member
Joined
Apr 15, 2002
Messages
869
Thanks, alan. But, I dont want to disable the button, I want to remove it, and it can be done. In the past, I have always disable it using your method but I think it looks a lot neater if the button is gone altogether.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,481
Messages
5,596,395
Members
414,063
Latest member
N_Bates

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
Top