VBA Progress Bar

What is Excel VBA Progress Bar?

An Excel VBA Progress Bar is a graphical user interface element that provides visual feedback on the progress of a task being performed within an Excel workbook. When you’re running a time-consuming VBA macro or a process that takes some time to complete, such as importing data, calculations, or data analysis, using a progress bar can enhance the user experience by showing the user that something is happening and indicating how far the task has progressed.

Consider the following example:

VBA Progress Bar Intro.jpg

This example demonstrates implementing the VBA Progress bar without Userform by using the Application.StatusBar to display the progress.

This program counts the progress of 100 steps displayed in the status bar in a percentage format. The application is made to wait for 1 second after every time. After all the steps are completed, a message box pops up, indicating to the user that all progress has been completed successfully. The output is shown below:

VBA Progress Bar Intro - Output.jpg
VBA Progress Bar Intro - Messagebox.jpg
Key Takeaways
  • The VBA Progress Bar offers visual feedback for task progression in Excel. It enhances user experience by making tasks feel responsive.
  • The Progress Bar can be implemented using a UserForm and the progress appearance and status can be updated through VBA code.
  • The width of the progress bar adjusts dynamically to show progress visually. The caption label on the progress bar displays the percentage or status.
  • The DoEvents function ensures real-time updates without UI freezing.

How to Create your own Progress bar?

To create your custom progress bar, follow the steps below:

Step 1: Go to the “Developer” section in the toolbar and click the “Visual Basic” option. Now, the VBA Editor opens to add functions and Sub procedures. Then, click the “Insert” and “Userform” buttons to create a new userform or blank page.

Developer - Visual Basic
VBA Progress Bar - Module - Userform

Step 2: Customize the properties of the UserForm as shown below:

VBA Progress Bar - Step 2 - Properties.jpg

Step 3: Insert a Frame and customize its properties.

VBA Progress Bar - Step 3 - Frame.jpg
VBA Progress Bar - Step 3 - Frame.jpg
VBA Progress Bar - Step 3 - Progress.jpg

Step 4: Create a Label to simulate the progress bar.

VBA Progress Bar - Step 4 - Label.jpg
VBA Progress Bar - Step 4 - Properties
VBA Progress Bar - Step 4 - Progress Bar.jpg

Step 5: Double-click the VBA Progress Bar UserForm to edit its private subroutines.

VBA Progress Bar - Step 5.jpg

Go to the drop-down and change the functionality from Click to Activate. It will show the UserForm subroutine as:

VBA Progress Bar - Step 5 - Activate.jpg

Step 6: Initialize a FOR-loop to simulate a progress bar.

VBA Progress Bar - Step 5 - For-Loop.jpg

In the FOR-loop, we make the application wait for 1 second. To simulate the progress, we slowly increase the width of the progress label defined in Step 4. The caption in the frame also increments in the FOR loop.

For the width to change, use the DoEvents function to perform VBA Progress Bar Control in real time.

Code:

In the UserForm:

Private Sub UserForm_Activate()
For x = 1 To Me.RectProgress.Width
Application.Wait Now + TimeValue(“0:00:01”)
FrameProgress.Caption = x
Me.RectProgress.Width = x
DoEvents
Next x
End Sub

Step 7: Run the UserForm. The output will be displayed below:

VBA Progress Bar - Step 7 - 201.jpg
VBA Progress Bar - Step 7 - 227.jpg
VBA Progress Bar - Step 7 - 234.jpg

Now that we know how to create a progress bar, let us see some examples below.


Excel VBA – All in One Courses Bundle (35+ Hours of Video Tutorials)

If you want to learn Excel and VBA professionally, then ​Excel VBA All in One Courses Bundle​ (35+ hours) is the perfect solution. Whether you’re a beginner or an experienced user, this bundle covers it all – from Basic Excel to Advanced Excel, Macros, Power Query, and VBA.

Examples

Given below are some examples of how to use the progress bar to check the progress of our tasks.

Example #1

Consider an example where you need to find the progress bar of a function. It can be done by declaring a UserForm as a progress bar.

Step 1: Create a subroutine to multiply 100 numbers.

VBA Progress Bar - Example 1 - Step 1.jpg

Step 2: Initialize a product variable to store the value of the product of 100 numbers and assign it as 1.

VBA Progress Bar - Example 1 - Step 2.jpg

Step 3: Initialize a for loop to run through 100 numbers and multiply them with x.

VBA Progress Bar - Example 1 - Step 3.jpg

Now that we have this function, let us see its progress bar.

Step 4: Go to the “Developer” tab in the toolbar and click the “Visual Basic” option. Now, the VBA Editor opens to add functions and Sub procedures. Then, click the “Insert” and “Userform” buttons to create a new userform or blank page.

Developer - Visual Basic
VBA Progress Bar - Module - Userform

Step 5: Customize the properties of the UserForm as shown below:

VBA Progress Bar - Example 1 - Step 5.jpg

There are no changes made here.

Step 6: Insert a Frame into the UserForm in VBA and customize its properties.

VBA Progress Bar - Step 3 - Frame.jpg
VBA Progress Bar - Example 1 - Step 6 - Frame Properties.jpg
VBA Progress Bar - Example 1 - Step 6 - Progress.jpg

Step 7: Create a Label to simulate the progress bar.

VBA Progress Bar - Example 1 - Step 7.jpg
VBA Progress Bar - Example 1 - Step 7 - label properties.jpg
VBA Progress Bar - Example 1 - Step 7 - Progress bar.jpg

Step 8: Double-click the VBA Progress Bar UserForm to edit its private subroutines.

VBA Progress Bar - Step 5.jpg

Go to the drop-down and change the functionality from Click to Activate. It will show the UserForm subroutine as:

VBA Progress Bar - Step 5 - Activate.jpg

Step 9: Initialize a FOR-loop to simulate a progress bar.

VBA Progress Bar - Example 1 - Step 9.jpg

In the FOR-loop, we call the Multiplication function declared earlier. We then make it wait for 1 second to simulate the progress since it is too fast otherwise. To simulate the progress, we slowly increased the width of the progress label defined earlier.

For the width to change, use the DoEvents function to perform VBA Progress Bar Control in real-time.

Code:

In the Module:

Sub MultiplyTill100()
Dim x As Double
x = 1
For i = 1 To 100
x = x * i
Next i
End Sub

In the UserForm:

Private Sub UserForm_Activate()
Dim currentStep As Long
For currentStep = 1 To Me.barwidth.Width
barframe.Caption = “Progress: ” & i
Call MultiplyTill100
Application.Wait Now + TimeValue(“0:00:01”)
Me.barwidth.Width = currentStep
DoEvents
Next currentStep
End Sub

Step 10: Run the UserForm. The output will be displayed below:

VBA Progress Bar - Example 1 - Step 10.jpg
VBA Progress Bar - Example 1 - Step 10 - Progress.jpg
VBA Progress Bar - Example 1 - Step 10 - Full progress

Example #2

Consider an example where you must create 10 worksheets with ten columns each. It can be done with a progress bar customized for your usage.

Step 1: Click the “Insert” and “Userform” buttons to create a new userform or blank page.

VBA Progress Bar - Module - Userform

Step 2: Go back to the Excel Worksheet and go to the “View” section. Here, tick off the gridlines.

VBA Progress Bar - Example 2 - Step 2.jpg

It will make the worksheet look like a blank unruled sheet.

VBA Progress Bar - Example 2 - Step 2 - unruled sheet.jpg

Step 3: Go to the “Insert” Section in the workbook and select your preferred shape.

VBA Progress Bar - Example 2 - Step 3.jpg

Step 4: After selecting the shape, add text to it by right-clicking it and clicking “Edit Text.”

VBA Progress Bar - Example 2 - Step 4.jpg

Step 5: Go to the VBA Progress Bar UserForm created earlier and customize its properties.

VBA Progress Bar - Example 2 - Step 5.jpg

It is important to keep “ShowModal” as “False.”

Step 6: Create a new frame and make it cover the userform.

VBA Progress Bar - Example 2 - Step 6.jpg
VBA Progress Bar - Example 2 - Step 6 - Frame.jpg

Step 7: Similarly, customize the properties of the frame in the Properties tab.

VBA Progress Bar - Example 2 - Step 7.jpg

Step 8: Create a label to show the progress bar.

Example 2 - Step 8.jpg
Example 2 - Step 8 - Label.jpg

Step 9: Change the properties of the label.

VBA Progress Bar - Example 2 - Step 9.jpg

Now the UserForm looks like this:

Example 2 - Step 9 - Userform.jpg

Step 10: Now, go to the “Insert” section and click the “Module” option.

VBA Progress Bar - Module.jpg

Step 11: Create a subroutine to create 10 worksheets and rename them as WS1, WS2 and so on.

Example 2 - Step 11.jpg

Step 12: Declare variables start, total number of sheets, the amount done, and the width of the label.

Example 2 - Step 12.jpg

Step 13: Declare the total number of sheets and width of the progress bar with the name of the label you mentioned earlier.

Example 2 - Step 13.jpg

Step 14: Initialize a FOR loop to add 10 worksheets from 1-10, and name ten columns in the sheets.

Example 2 - Step 14.jpg

Here, the Application is made to wait for 1 second after every loop is complete. Also, change the worksheet names using the NAME function in Excel VBA.

Keep the progress bar value with the percentage done value.

Step 15: Change the width of the UserForm Progress label with the percentage done value. Also, change the caption of the Progress label as the progress increases.

Example 2 - Step 15.jpg

Step 16: Create another subroutine to show the VBA Progress Bar UserForm.

Example 2 - Step 16.jpg

Step 17: Now, go to the UserForm, and double-click it.

Example 2 - Step 17.jpg

You will get Private Sub UserForm_Click(). Click the drop-down section and select Activate.

Example 2 - Step 16 - Activate.jpg

Now, you will get the subroutine as shown below:

Example 2 - Step 17 - Userform.jpg

Step 18: Set the width of the progress label as 0 and call the ActivityProcess subroutine.

Example 2 - Step 18.jpg

Step 19: Go to the shape installed earlier in Step 4 and click Assign Macro.

Example 2 - Step 19.jpg

Step 20: Select the Show_form() subroutine.

Example 2 - Step 20.jpg

Now, if you click “Insert Worksheets”, you will initialize VBA Progress Bar while macro is running.

Code:

In the module:

Sub ActivityProcess()
Dim st As Integer
Dim totsheet As Integer
Dim pctDone As Single
Dim iLabelWidth As Integer
totsheet = 10
iLabelWidth = 240
For st = 1 To totsheet
Sheets.Add(after:=Sheets(Sheets.Count)).Name = “WS-” & st
Sheets(Sheets.Count).Range(“A1”).Value = “COLUMN 1”
Sheets(Sheets.Count).Range(“B1”).Value = “COLUMN 2”
Sheets(Sheets.Count).Range(“C1”).Value = “COLUMN 3”
Sheets(Sheets.Count).Range(“D1”).Value = “COLUMN 4”
Sheets(Sheets.Count).Range(“E1”).Value = “COLUMN 5”
Sheets(Sheets.Count).Range(“F1”).Value = “COLUMN 6”
Sheets(Sheets.Count).Range(“G1”).Value = “COLUMN 7”
Sheets(Sheets.Count).Range(“H1”).Value = “COLUMN 8”
Sheets(Sheets.Count).Range(“I1”).Value = “COLUMN 9”
Sheets(Sheets.Count).Range(“J1”).Value = “COLUMN 10”
Application.Wait Now + TimeValue(“0:00:01”)
pctDone = st / totsheet
ProgressForm.lblprogress.Width = iLabelWidth * pctDone
ProgressForm.FrameProgress.Caption = Format(pctDone, “0%”)
DoEvents
Application.Wait Now + TimeValue(“0:00:01”)
Next st
Unload ProgressForm
End Sub

Sub Show_Form()
ProgressForm.Show
End Sub

In the UserForm:

Private Sub UserForm_Activate()
Me.lblprogress.Width = 0
Call ActivityProcess
End Sub

Step 21: After clicking the “Insert Worksheets” button, the output is shown below.

Example 2 - Step 21 - Intial Progress.jpg
Example 2 - Step 21 - Progress
Example 2 - Step 21 - Sheet.jpg

As you can see from the progress bar, we can see how many worksheets have been printed with Columns 1-10.

Important Things To Note

  • Ensure that the progress bar accurately reflects the progress of the task. You can use labels, tooltips, or other means to explain the task and why it might take time.
  • If applicable, consider displaying progress in meaningful units rather than just percentages. For example, show the record number being processed if you’re processing records.
  • Use the DoEvents statement after updating the progress bar to allow the UserForm to refresh in real time.
  • Avoid progress bars that move too quickly or too slowly. The progress should accurately reflect the actual progress of the task.
  • Avoid long operations that block the UI completely.

Frequently Asked Questions (FAQs)

1. Why is VBA Progress Bar Control not working?

Incorrect Code: Check if your VBA code has errors or is not properly updating the progress bar.
UI Blocking: If the task is blocking the UI, the progress bar won’t update until the task completes.
Wrong References: Ensure you’re referencing the correct controls and objects in your code.
Missing DoEvents: Without DoEvents, the UI won’t update in real-time during long tasks.
Incorrect Properties: Verify the properties (width, caption) are being set correctly.
Form Not Shown: If the form isn’t shown, the progress bar won’t be visible.

2. What is the use of progress bar in Visual Basic?

A progress bar in Visual Basic provides visual feedback on the status or completion of a task, improving user experience by showing ongoing processes and indicating how far along they’ve progressed.

3. How do I change the color of the progress bar in VBA?

Select the progress bar.
In properties, locate “BackColor” or “FillColor.”
Choose a color using the color picker or go to the “Pallete” section.

VBA Progress Bar - Color picker.jpg

Download Template

This article must be helpful to understand the VBA PROGRESS BAR, with its formula and examples. You can download the template here to use it instantly.

This has been a guide to VBA Progress Bar in Excel. Here we explain how to create our own custom progress bar to check the progress of task with examples and downloadable template. You may learn more from the following articles –

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *