Excel

Fade Transparency Gradually - Splash Screen - on a Userform

Ease of Use

Intermediate

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This macro will slowly fade out a UserForm. 

Discussion:

Perfect for a Splash Screen. This macro will slowly fade the transparency of a UserForm until it is completely invisible, then it will be closed. 

Code:

instructions for use

			

'Place this code in a Module Sub ShowForm() UserForm1.Show End Sub 'Place this code in a UserForm with one Command Button named CommandButton1. Option Explicit Private Declare Function GetActiveWindow Lib "USER32" () As Long Private Declare Function SetWindowLong Lib "USER32" Alias "SetWindowLongA" _ (ByVal hWnd As Long, _ ByVal lngWinIdx As Long, _ ByVal dwNewLong As Long) As Long Private Declare Function GetWindowLong Lib "USER32" Alias "GetWindowLongA" _ (ByVal hWnd As Long, _ ByVal lngWinIdx As Long) As Long Private Declare Function SetLayeredWindowAttributes Lib "USER32" _ (ByVal hWnd As Long, _ ByVal crKey As Integer, _ ByVal bAlpha As Integer, _ ByVal dwFlags As Long) As Long Private Const WS_EX_LAYERED = &H80000 Private Const LWA_ALPHA = &H2 Private Const GWL_EXSTYLE = &HFFEC Dim hWnd As Long Dim Transparancy As Integer Dim Running As Boolean Private Sub CommandButton1_Click() 'Close Unload Me End Sub Private Sub UserForm_Activate() Running = True Call Transparency End Sub Private Sub Transparency() Dim MyTimer As Double DoEvents MyTimer = Timer Do Do Loop While Timer - MyTimer < 0.07 MyTimer = Timer Transparancy = Transparancy - 1 If Transparancy < 0 Then Unload Me Else Call SemiTransparent(Application.WorksheetFunction.Min(Transparancy, 100)) End If DoEvents Loop While Running End Sub Private Sub SemiTransparent(ByVal intLevel As Integer) Dim lngWinIdx As Long hWnd = GetActiveWindow lngWinIdx = GetWindowLong(hWnd, GWL_EXSTYLE) SetWindowLong hWnd, GWL_EXSTYLE, lngWinIdx Or WS_EX_LAYERED SetLayeredWindowAttributes hWnd, 0, (255 * intLevel) / 100, LWA_ALPHA End Sub Private Sub UserForm_Initialize() Transparancy = 120 Call SemiTransparent(100) DoEvents End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) Running = False 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:

UserForm Fade Transparency.zip 9.06KB 

Approved by mdmackillop


This entry has been viewed 595 times.

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