Quick Jump
Introduction | Example | Tutorial | Applications | Comments

Introduction - Borderless UserForm

This tutorial shows you how to hide the border on your UserForm using VBA. A UserForm without a title bar and X button are far cleaner than normal UserForms. Creating a UserForm like this is great for splash screens and catchy interfaces.

Keep reading this VBA tutorial to learn how you can make prettier UserForms by removing the ugly title bar at the top and border around the edges.


Example - Borderless UserForm

Option Explicit
Option Private Module

Public Const GWL_STYLE = -16
Public Const WS_CAPTION = &HC00000
#If VBA7 Then
    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
    Public Declare PtrSafe Function FindWindowA _
                           Lib "user32" (ByVal lpClassName As String, _
                           ByVal lpWindowName As String) As Long
#Else
    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
    Public Declare Function FindWindowA _
                           Lib "user32" (ByVal lpClassName As String, _
                           ByVal lpWindowName As String) As Long
#End If
Sub HideTitleBar(frm As Object)
    Dim lngWindow As Long
    Dim lFrmHdl As Long
    lFrmHdl = FindWindowA(vbNullString, frm.Caption)
    lngWindow = GetWindowLong(lFrmHdl, GWL_STYLE)
    lngWindow = lngWindow And (Not WS_CAPTION)
    Call SetWindowLong(lFrmHdl, GWL_STYLE, lngWindow)
    Call DrawMenuBar(lFrmHdl)
End Sub

Your time is valuable. It's time to become a VBA expert.

Tutorial - Borderless UserForm

Hiding Your Title Bar

In this tutorial, I’m going to assume you already have your UserForm designed. Once you’ve copied and pasted the example macro into a regular module, all you have to do is go to the code for your UserForm and paste the following macro:

Private Sub UserForm_Initialize()
    HideTitleBar Me
End Sub

Reminder: To access the code for your UserForm, right click your UserForm in the Project Explorer Pane and click “View Code.”

This feature will work with both 32-bit and 64-bit versions of Excel. Each time your UserForm is started, it invokes the UserForm_Initialize subroutine and hides the title bar. Your left with a beautiful borderless UserForm like the one below:

Borderless UserForm - VBA Remove Title Bar

If you’re wondering where this UserForm came from, it’s from my PDF VBA Excel Add-in - a really cool add-in for converting your Excel creations to sleek PDFs.

One thing you may notice when you launch your UserForm is that all the elements of your UserForm are shifted up higher than they appear when you’re viewing your UserForm in design mode. That’s because each element is shifted up by the original height of the title bar.

To compensate for this, all you have to do is shorten the height of your UserForm.


Application Ideas - Borderless UserForm

As I said in my introduction, creating a UserForm without borders is perfect for splash screens, but it’s also great for an “About Me” page and other interactive designs, like calendars.

What beautiful borderless UserForms can you create?


Comments

Occassionally I’m asked how people can help keep the VBA Tutorials Blog online. The best thing you can do is purchase the add-ins on my Excel Add-ins page. This is my primary source of blog revenue and the money I receive from sales helps pay to keep this website online and accessible for all to learn.

If you haven’t already done so, please visit my Excel Add-ins page and see how my products can help you!

Now’s the time I kindly remind you to subscribe to my email list. Once you’ve subscribed, you’ll get access to a free copy of my Excel Add-In with tons of useful features and you’ll get monthly tutorials where I show you how to do some pretty cool stuff with VBA:)

Share this article with the world on Google+, Twitter and Facebook!

Discover how this Nuclear Engineer Mastered Excel VBA

And why you should, too


Your time is valuable. It's time to become a VBA expert.

About Ryan Wells


Ryan Wells

Ryan Wells is a Nuclear Engineer and professional VBA Developer. He is the lead developer of several VBA applications, including PDF VBA - a leading Excel Add-in for exporting Excel Objects, like charts and tables, to PDFs. Discover more of his popular Excel Add-ins, including Mouse To Macro and CF Shapes, at his dedicated Excel Add-ins page.