Tutorial Details
Downloadable Files: Excel File
Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Transparent UserForm

Add to Favorites
Author: | Edits: don

Make a UserForm transparent in Excel; this allows you to see what is in the worksheet behind the form and can make things like data entry a bit easier.

a4a3aca85647644480f70aa49791c6a4.jpg

Sections:

Make UserForm Transparent

Set Form Opacity with a Button

Notes

Make UserForm Transparent

To do this, we need to use VBA code. This code will go into a regular module and also within the Initialize event of the UserForm.

Module Code

This goes into a regular module within the VBA window. (Alt + F11 to get to the VBA window and then Insert > Module.)

Private Declare Function FindWindow _
    Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong _
    Lib "user32" Alias "GetWindowLongA" _
    (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong _
    Lib "user32" Alias "SetWindowLongA" _
    (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes _
    Lib "user32" _
    (ByVal hWnd As Long, ByVal crey As Byte, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Const LWA_ALPHA = &H2&

Public hWnd As Long

Note: the above code must go at the very top of the module.

This next section of code can be placed anywhere within a module.

Sub MakeTransparent(frm As Object, TransparentValue As Integer)

Dim bytOpacity As Byte

'Control the opacity setting.
bytOpacity = TransparentValue

hWnd = FindWindow("ThunderDFrame", frm.Caption)
Call SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) Or WS_EX_LAYERED)
Call SetLayeredWindowAttributes(hWnd, 0, bytOpacity, LWA_ALPHA)

End Sub

UserForm Code

This line of code must be placed within the UserForm itself in order for it to work.

Place this within the Initialize event.

MakeTransparent Me, 200

200 is the only thing you should change as it determines the level of opactiy. Any value between 165 and 200 generally works well.

Opacity Values:

  • Max number: 255 (opaque)
  • Min value: 0 (transparent)

The code within the Initialize event, would look like this:

Private Sub UserForm_Initialize()

MakeTransparent Me, 200

End Sub

If you already have code in the Initialize event, just place MakeTransparent Me, 200 at the top of this section.

To learn more about events and how to use them, view our tutoral on UserForm Events.

Set Form Opacity with a Button

You can make it so that the opacity of a form is changed depending on what is happening within the form. This could be set with the click of a button, or any other trigger within the form.

For this example, I added two buttons on the form, one will make the form transparent and the other will make it opaque.

4a64c81f2e8d7e6ce37abfad17fd7df7.jpg

Transparent code:

Private Sub CommandButton4_Click()

'Make the form transparent.
MakeTransparent Me, 200

End Sub

CommandButton4 is the name of the Transparent button.

Opaque code:

Private Sub CommandButton5_Click()

'Make the form opaque.
MakeTransparent Me, 255

End Sub

CommandButton5 is the name of the Opaque button.

Note: this code goes into the code window for the UserForm. To get to each section of code for the buttons, simply double-click the button after you have put it onto the form.

Once you click the Transparent button, it looks like this:

ef3579a600f833d1a5855d561b3427ac.jpg

Click the Opaque button to get back to the default, non-transparent form.

Notes

Make sure not to make the form completely invisible or it might be difficult to actually close it. I suggest keeping your opacity levels set to just enough so that the worksheet behind the form can be read.

Download the sample file for this tutorial to see the above examples in Excel.

Question? Ask it in our Excel Forum


Downloadable Files: Excel File