Remove the Title Bar from a UserForm
Remove the Title Bar and border from a form, including the red X, or close button.
This creates a clean UserForm interface and is great for things like splash pages or just giving your forms a cleaner and more streamlined appearance.
Sections:
Remove Title Bar from UserForm
Remove Title Bar from UserForm
To remove the title bar and border, we need to add VBA code into a regular module and then call that code from the UserForm_Initialize event, which is what runs when a form is opened.
Module Code
The below code goes into a regular module in the VBA window. (Alt + F11 to get to the VBA window and then Insert > Module.)
Option Explicit
#If VBA7 Then
Public Declare PtrSafe Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Public Declare PtrSafe Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Public Declare PtrSafe Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Declare PtrSafe Function DrawMenuBar Lib "user32" _
(ByVal hWnd As Long) As Long
#Else
Public Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Public Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Declare Function DrawMenuBar Lib "user32" _
(ByVal hWnd As Long) As Long
#End If
Note: this code must go at the very top of the module before any other code!
The next section of code can be placed anywhere within the module.
Sub HideBar(frm As Object)
Dim Style As Long, Menu As Long, hWndForm As Long
hWndForm = FindWindow("ThunderDFrame", frm.Caption)
Style = GetWindowLong(hWndForm, &HFFF0)
Style = Style And Not &HC00000
SetWindowLong hWndForm, &HFFF0, Style
DrawMenuBar hWndForm
End Sub
UserForm Code
Code must also be placed within the UserForm itself in order to work.
All you need to do is call HideBar and pass it a reference to the form.
HideBar Me
This piece of code should be placed inside the UserForm_Initialize event. This event will usually have other code inside of it, but the most basic version of the event for this example could look like this:
Private Sub UserForm_Initialize()
'Remove Border and Title Bar
HideBar Me
End Sub
To learn more about the Initialize event and events in general, view our tutoral on UserForm Events.
Notes
Removing the title bar removes the default close button on the form; as such, you should place a button somehwere on the form that will allow the user to close it.
This also removes the ability to move the form around the window with your mouse; as such, you should think about this before implementing this code or you should use additional code that allows the user to move the form by clicking on it and dragging it without a title bar.
This code works great with splash screens in Excel but you can use it any time you want your form to have a more streamlined look.
Don't forget to download the sample file to see this example in Excel.