VBA Animation in UserForm (4 Suitable Examples)

Example 1 – Creating an Animated Clock in UserForm

We’ll create an Animated Clock that will look like this.

Animated Digital Date & Clock in UserForm

Steps:

  • Go to the Developer tab and open the Microsoft Visual Basic for Application window by clicking the Visual Basic command. Shortcut key is Alt+F11.

Opening Visual Basic Window

  • In the Microsoft Visual Basic for Application window, select Insert >> UserForm.

Inserting A New VBA UserForm for animation

  • A new userform will be created.

Newly Created VBA UserForm for animation

  • We will insert different tools from the Toolbox. We need 3 Labels and 1 to insert a Label. Click on the Label icon on the Toolbox and drag it to your desired position on the UserForm.

Inserting a Label

  • Drag the other two Labels and place them below the first Label.

Inserting Two New Labels in VBA UserForm for animation

  • We will Insert a CommandButton.

Inserting CommandButton

  • We need to modify the Properties of each inserted elements. Click on the Label1 and open the property tab by going to View >> Properties Window (if the Properties Window is not available).

Opening the Properties Tab

  • The Properties tab should be visible in the left corner. We will edit the Caption of Label1.

Editing Caption of the Label1

  • We will change the Font Style and Font Size of Label1. Click on the 3 dots on the Font option and choose font style and size from the pop-up window.

Editing the Font Style of Label1

  • From the popup window, we have chosen the following Font style and size.

Choosing Font Style of Label1

  • To make the UserForm more attractive, change the ForeColor. We have chosen Green from the color palette.

Choosing Font Color

  • Change the Properties for the rest of the components as per your requirements. We have formatted all the components as shown in the following image.

Final Formatting of the UserForm

  • Label3 will scroll when we run the UserForm.
  • Double-click on any part of the UserForm to open the code window.

VBA Code for Date and Clock animation in UserForm

VBA Code:

Private Sub CommandButton1_Click()
End
End Sub
Private Sub UserForm_Activate()
On Error Resume Next
Do
Label1 = Format(Date, "dddd, dd mmm yyyy")
Label2 = Format(Time, "hh:mm:ss AM/PM")
Label3.Left = Label3.Left - 2
If Label3.Left <= 0 - Label3.Width Then Label3.Left = Me.Width
For i = 1 To 8000000
Next
DoEvents
Loop
End Sub

How Does the Code Work?

Private Sub CommandButton1_Click()
End
End Sub

This code is activated when CommandButton1 is clicked. It ends all the running Subroutine.

Private Sub UserForm_Activate()

The subroutine will run whenever the UserForm will be activated.

On Error Resume Next

The code will neglect any error and resume the next line.

Label1 = Format(Date, "dddd, dd mmm yyyy")

In Label1 the Current Date will be shown and its format wil be “dddd, dd mmm yyyy”. You can modify the format to display according to your choice of format.

Label2 = Format(Time, "hh:mm:ss AM/PM")

In Label2 the current time will be shown and its format will be “hh:mm:ss AM/PM”. Like ythe Label1, you can also modify the format.

Left = Label3.Left - 2
If Label3.Left <= 0 - Label3.Width Then Label3.Left = Me.Width

The position of Label3 is continuously shifted to the left of the UserForm until it reaches at the left end. When its position is on the Left edge of the UserForm, the position of Label3 is shifted towards the right end of the UserForm. When the code will run under a For loop, it will look like a scrolling text.

For i = 1 To 8000000
Next
DoEvents

The above code is executed within a For loop. Hence, it becomes dynamic. You can decrease the upper limit of the iteration variable (i) to make the scrolling fast.

  • If you run the UserForm, you will see the Dynamic UserForm with Date, Time and Scrolling Text.

Animated Digital Date & Clock in UserForm


Example 2 – Create Vertical Scrolling Text in VBA UserForm

Let’s learn how to display a vertical scrolling text in UserForm. The Vertical Text will be as shown below.

Vertical Scrolling Text

Steps:

  • Insert a new UserForm and inside the UserForm, insert a Label and a CommandButton following the first method. Edit the caption according to your needs. In Label1, we will enter the text that we want to scroll in the caption.

Inserted Label and CommandButton

  • After rewriting the caption of those components, open the code window by double-clicking any part of the UserForm. Enter the following code in the code window.

VBA Code for Vertical Scrolling Text animation in UserForm

VBA Code Syntax:

Private Sub UserForm_Activate()
On Error Resume Next
Do
Label1.Top = Label1.Top - 1
If Label1.Top <= 0 - Label1.Height Then Label1.Top = UserForm2.Height
For i = 1 To 5000000
Next
DoEvents
Loop
End Sub
Private Sub CommandButton1_Click()
End
End Sub

How Does the Code Work?

Do
Label1.Top = Label1.Top - 1
If Label1.Top <= 0 - Label1.Height Then Label1.Top = UserForm2.Height
For i = 1 To 5000000
Next
DoEvents
Loop

The code uses a Do loop. Inside the Do loop, the vertical position of Label1 is decreased until it reaches the Top edge of the UserForm. The Vertical position of Label1 is set to the height of the UserForm. This loop continues which makes the label a scrolling text. If you want to increase the speed of the scrolling text, we need to decrease the upper value of iteration variable i.

Private Sub CommandButton1_Click()
End
End Sub

This subroutine will run when we click the CommandButton1. When we click the button, it will end all the running subroutines.

  • When we run the code, we will see the following output.

Vertical Scrolling Text


Example 3 – Creating Animation Image in UserForm

We will create an animation image in UserForm as shown below.

Superman Flying in the UserForm

Steps:

  • Insert a new UserForm. In the UserForm, insert an Image and a CommandButton from the Toolbox. Edit the caption of the CommandButton according to your needs.

Inserting CommandButton & Image

  • Insert your desired image.

Opening Load Picture Window

  • Select the picture from the pop-up window, Load Picture.

Selecting the Picture from Load Picture Window

  • You will be able to see the selected picture on the UserForm. To accommodate the picture inside the Image tool, resize it inside the tool.

Selected Picture inside the UserForm

  • Change the Background color of the UserForm based on the background of the image. So that when the image is scrolling, it will appear as an integral part of the UserForm. As the background color of our selected image is white, we have change the color of the UserForm to white.

Changing the Background Color of the UserForm to the Background Color of the Selected Image

  • If the image has any border, we can remove it by changing the BorderStyle to 0-fmBorderStyleNone.

Changing the Border Style of the Image in VBA userform for animation

  • Our image is be ready for animation.

Ready Image for Animation in VBA userform

  • Double-click anywhere on the UserForm to open the Code Window.
  • Clear all the existing code.
  • Enter the following code in the window.

VBA Code for Scrolling Image animation on UserForm

Code Syntax:

Private Sub UserForm_Activate()
On Error Resume Next
Do
Image1.Left = Image1.Left - 1
If Image1.Left <= 0 - Image1.Width Then Image1.Left = Me.Width
For i = 1 To 8000000
Next
DoEvents
Loop
End Sub
Private Sub CommandButton1_Click()
End
End Sub

How Does the Code Work?

Do
Image1.Left = Image1.Left - 1
If Image1.Left <= 0 - Image1.Width Then Image1.Left = Me.Width
For i = 1 To 8000000
Next
DoEvents
Loop

This code will shift the image toward the left direction as long as its right corner reaches the UserForm’s left edge. When it reaches the Left edge of the UserForm, the code then brings the left corner of Image1 to the right edge of the UserForm. This loop continues.

Private Sub CommandButton1_Click()
End
End Sub

When we click the CommandButton1, it will end all the running subroutines.

  • If we run the UserForm, we will see the scrolling image on the UserForm which is our VBA animation output in UserForm.

Superman Flying in the UserForm


Example 4 – Slideshow Using VBA UserForm

We will make a slideshow of images as shown below.

SlideShow Using UserForm in Excel

Steps:

  • Insert a new UserForm. In this UserForm, insert 2 CommandButtons and 1 Image from the ToolBox.
  • Label the After labeling it, the Layout of the UserForm will look like the following image.

VBA UserForm Layout for Scrolling Image animation

  • Open the code window by double-clicking any part of the UserForm. Enter the following code in the code window.

VBA Code for SlideShowing Images animation on UserForm

Code Syntax:

Private Sub CommandButton1_Click()
Set Pic_File = Application.FileDialog(msoFileDialogFolderPicker)
With Pic_File
    .Title = "Select a Folder where pictures are"
    If .Show <> -1 Then GoTo Exit_Sub
    My_Path = .SelectedItems(1)
End With
Set Img_Obj = CreateObject("Scripting.FilesystemObject")
Set Img_Folder = Img_Obj.GetFolder(My_Path)
Do Until a = 1
 For Each Obj_File In Img_Folder.Files
 DoEvents
 If Obj_File.Type = "JPG File" Then
 Photo_Var = Photo_Var + 1
 Me.Image1.Picture = LoadPicture(Obj_File.Path)
 Me.Image1.PictureSizeMode = fmPictureSizeModeStretch
 Application.Wait Now + 0.00001
 End If
 Next
 Loop
Exit_Sub:
End Sub
Private Sub CommandButton2_Click()
End
End Sub

How Does the Code Work?

Private Sub CommandButton1_Click()

This subroutine will run when the ComamndButton1 is clicked.

Set Pic_File = Application.FileDialog(msoFileDialogFolderPicker)
With Pic_File
    .Title = "Select a Folder where pictures are"
    If .Show <> -1 Then GoTo Exit_Sub
    My_Path = .SelectedItems(1)
End With

A FileDialoge box will ask the User to select a Folder where the pictures are. If nothing is selected then the subroutine will exit.

Set Img_Obj = CreateObject("Scripting.FilesystemObject")
Set Img_Folder = Img_Obj.GetFolder(My_Path)

The Scripting.FileSystemObject is an object in VBA that provides access to the computer’s file system. Img_Folder stores the Folder Path of the selected folder.

Do Until a = 1
.
.
Loop

A Do Until loop is introduced. A new random variable a is taken which was never initialized before. The Do Until loop is essentially an infinite loop as the value of a will never reach 1 in the future.

For Each Obj_File In Img_Folder.Files
 DoEvents
.
.
Next

For Each loop is used to loop through each file in the selected folder. DoEvents will allow the user to interact with the interface while the macro is running.

If Obj_File.Type = "JPG File" Then
.
.
End If

An If statement is used to determine whether the file is a JPG file. We will only take the JPG file as the UserForms in Excel can only load JPG type images.

Me.Image1.Picture = LoadPicture(Obj_File.Path)

If the condition is True then the image file is load to Image1 in UserForm.

Me.Image1.PictureSizeMode = fmPictureSizeModeStretch

The JPG file is stretched across the whole area of image1 in the UserForm.

Application.Wait Now + 0.00001

The next step is delayed by 1 second.

Private Sub CommandButton2_Click()
End
End Sub

This code will run when the CommandButton2 is clicked. It will end all the running subroutines.

  • After running the UserForm, click on the Select Folder (CommandButton1).

Clicking on Select Folder

  • A dialog box will open and promptly ask the user to select a folder. Select the folder where the images in JPG formats are stored. Click OK.

Selecting Folder

  • A slideshow will be created consisting of all the JPG files in that folder. This is the final VBA animation output in UserForm.

SlideShow Using UserForm in Excel


Things to Remember

  • Be aware of the Name of the Components (Labels, CommandButtons, Images). If the names are not the same as shown in the examples above, the provided code may not work.
  • Always resize the UserForms and components as required.

Download Practice Workbook

Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Aniruddah Alam
Aniruddah Alam

Md. Aniruddah Alam, a graduate of Bangladesh University of Engineering & Technology, Dhaka, holds a BSc.Engg in Naval Architecture & Marine Engineering. Joining Softeko in August 2022 as an Excel and VBA content developer, he has authored 90+ articles, covering fundamental to advanced Excel topics. He actively develops VBA codes for Excel automation and offers solutions in the Exceldemy Forum. Beyond work, he finds leisure in reading books, and taking relaxing walks, showcasing well-rounded blend of professional and... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo