Excel

Expand into view a Modeless UserForm.

Ease of Use

Intermediate

Version tested with

2000,2002 

Submitted by:

Marcster

Description:

This example will slowly expand into view a Modeless UserForm. 

Discussion:

This example will show a Modeless UserForm differently to the usual default way. It will display a Modeless UserForm in a more impressive way by animating it to expand outwards till it reacheas it's set size on the screen. It uses the API function AnimateWindow to do this. You could use this as a novel way to display a Modeless UserForm. 

Code:

instructions for use

			

'***The following code should be placed in a standard module***. Option Explicit Sub ShowExpandingUserForm() 'Load UserForm1 into memory, this doesn't show the form. 'It needs to be hidden from view in order for the AnimateWindow 'function to run correctly. Load UserForm1 End Sub '***The following code should be placed in the code module of the UserForm*** Option Explicit Private Declare Function AnimateWindow Lib "user32" (ByVal hwnd As Long, _ ByVal dwTime As Long, _ ByVal dwFlags As Long) As Boolean Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long Const AW_CENTER = &H10 'Makes the UserForm appear to expand outwards. Const AW_ACTIVATE = &H20000 'Activates the window. Private Sub CommandButton1_Click() Unload Me 'Remove UserForm1 from memory End Sub Private Sub UserForm_Initialize() 'The form needs to be already hidden, 'this has been done by using 'Load UserForm1 'by clicking on the button on Sheet1 or running Macro 'ShowExpandingUserForm' 'Set where you want the UserForm to appear at With UserForm1 .Top = 250 'Change 250 to what you want the UserForm to be displayed .Left = 250 'Change 250 to what you want the UserForm to be displayed End With Dim lngFrmWindow As Long 'Retrieve a handle to UserForm1 'ThunderDFrame is the Class name for Excel 2000/2002 UserForm 'UserForm1 is the name of the Window we want to animate lngFrmWindow = FindWindow("ThunderDFrame", "Userform1") If lngFrmWindow <> 0 Then 'lngFrmWindow returns a zero if there's an error 'Make the form expand outwards and become active '750 is in milliseconds the time length of the animation AnimateWindow lngFrmWindow, 750, AW_CENTER Or AW_ACTIVATE 'Repaint the userform so it shows the form's contents. Me.Repaint Else 'An error has occured. Call MsgBox("There was an error when trying to animate the userform." _ & vbCrLf & "" _ , vbCritical, Application.Name) End If 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 a standard module.
  5. Insert | UserForm.
  6. Double click the userform and paste the code from above designated for the UserForm module.
  7. Add one command button to the UserForm from the Control Toolbox.
  8. This will enable you close the UserForm.
  9. Close the VBE (Alt + Q or press the X in the top right corner).
  10. Run Macro named 'ShowFadingForm' by
  11. Tolls | Macro | Macros...
 

Test the code:

  1. On Sheet1 of the attached workbook, click the button 'Show UserForm1'.
 

Sample File:

ExpandIntoViewUserForm.zip 16.24KB 

Approved by mdmackillop


This entry has been viewed 343 times.

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