Excel

Disallow Movement of a Userform

Ease of Use

Intermediate

Version tested with

2000, 2002 

Submitted by:

Jacob Hilderbrand

Description:

This macro will stop a user from being able to move a UserForm. 

Discussion:

You have a userform, and you don't want it to be moved from where you place it. This macro will disable movement of the userform. 

Code:

instructions for use

			

'Place this code in a Module Option Explicit Private Declare Function GetSystemMenu Lib "USER32" _ (ByVal hWnd As Long, _ ByVal bRevert As Long) As Long Private Declare Function RemoveMenu Lib "USER32" _ (ByVal hme2nu As Long, _ ByVal nPosition As Long, _ ByVal wFlags As Long) As Long Private Declare Function FindWindowA Lib "USER32" _ (ByVal lpClassName2 As String, _ ByVal lpWindowName2 As String) As Long Private Const MF_BYPOSITION As Long = &H400 Public Sub FormatUserForm(UserFormCaption As String) Dim lFrmHdl As Long Dim iCount As Integer lFrmHdl = FindWindowA(vbNullString, UserFormCaption) If lFrmHdl <> 0 Then For iCount = 0 To 1 RemoveMenu GetSystemMenu(lFrmHdl, False), 0, MF_BYPOSITION Next iCount End If End Sub Sub ShowForm() UserForm1.Show End Sub 'Place this code in a UserForm with one Command Button named CommandButton1. Option Explicit Private Sub CommandButton1_Click() Unload Me End Sub Private Sub UserForm_Initialize() Call FormatUserForm(Me.Caption) End Sub

How to use:

  1. Open Excel.
  2. Alt + F11 to open the VBE.
  3. Insert | Module.
  4. Paste the code from above that is designated for the Module.
  5. Insert | UserForm.
  6. Double click the userform and paste the code from above designated for the UserForm.
  7. Add one command button to the UserForm from the Control Toolbox.
  8. Close the VBE (Alt + Q or press the X in the top right corner).
 

Test the code:

  1. Tools | Macro | Macros...
  2. Select ShowForm and press Run.
 

Sample File:

Stop UserForm Movement.zip 9.75KB 

Approved by mdmackillop


This entry has been viewed 262 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express