Add a Progress bar to a user form

MarkCBB

Active Member
Joined
Apr 12, 2010
Messages
497
HI there VBA Guru's

I am using the following code to filter some Pivot Tables on a worksheet, however it takes sometime to run the marco, I would like to have a progress bar in the user form to show the progress of the macro. however I have not been able to insert a progress bar into the user form.

Below is the code that I am using:

Code:
Private Sub CommandButton1_Click()
Dim MyRegion As String
Dim i As Integer
Dim P_count As Integer

P_count = ActiveSheet.PivotTables.Count
i = 1
Range("A2").Value = ListBox1.Value
MyRegion = Range("A2").Text
Do
    With Sheets("Report").PivotTables("PivotTable" & i).PivotFields("Client Region")
        .PivotItems(MyRegion).Visible = True
        For Each Pi In .PivotItems
           If Pi.Name <> MyRegion Then Pi.Visible = False
         Next Pi
    End With
    i = i + 1
Loop Until i = P_count + 1
End Sub

Private Sub UserForm_Initialize()
With ListBox1
    .AddItem "Central"
    .AddItem "Eastern Cape"
    .AddItem "Kwa-Zulu Natal"
    .AddItem "Limpopo"
    .AddItem "Mpumalanga"
    .AddItem "Northern Gauteng"
    .AddItem "Southern Gauteng"
    .AddItem "Western Cape"
End With
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Adding a progress bar to a user form isn't too much of a problem in itself. Right click the controls toolbox and choose 'additional controls'. You need to select 'microsoft progressbar v6.0' from the list.

Where you may experience problems is how you update it. If you're in a control loop of vb code you can achieve this more easily, but if your process is dependant on inbuilt functions and it's these that are taking the time then updating the progress bar in your code to show different levels of completion wont' be possible.
 
Upvote 0
Hi there Weaver,

thanks for your help, I got it working, looks pretty cool. thank you. below is the updated code:

Code:
Private Sub CommandButton1_Click()
Dim MyRegion As String
Dim i As Integer
Dim P_count As Integer
Dim q As Integer
Dim P_Loop As Integer
Dim x As Integer

P_count = ActiveSheet.PivotTables.Count
i = 1
Range("A2").Value = ListBox1.Value
MyRegion = Range("A2").Text
ProgressBar1.Visible = True
q = 0
x = 1
P_Loop = P_count * Sheets("Report").PivotTables("PivotTable" & i).PivotFields("Client Region").PivotItems.Count

Do
    With Sheets("Report").PivotTables("PivotTable" & i).PivotFields("Client Region")
        .PivotItems(MyRegion).Visible = True
        For Each Pi In .PivotItems
           If Pi.Name <> MyRegion Then Pi.Visible = False
           
           q = x / P_Loop * 100
           ProgressBar1.Value = q
           x = x + 1
         Next Pi
    End With
    i = i + 1
Loop Until i = P_count + 1
q = x / P_Loop * 100
End Sub
Private Sub UserForm_Initialize()
With ListBox1
    .AddItem "Central"
    .AddItem "Eastern Cape"
    .AddItem "Kwa-Zulu Natal"
    .AddItem "Limpopo"
    .AddItem "Mpumalanga"
    .AddItem "Northern Gauteng"
    .AddItem "Southern Gauteng"
    .AddItem "Western Cape"
End With
End Sub
 
Upvote 0
Quick question, how can I make the form unload it sleft after the command button is pushed?
 
Upvote 0
Try unload commnad as below

Hi there Weaver,

thanks for your help, I got it working, looks pretty cool. thank you. below is the updated code:

Code:
Private Sub CommandButton1_Click()
Dim MyRegion As String
Dim i As Integer
Dim P_count As Integer
Dim q As Integer
Dim P_Loop As Integer
Dim x As Integer

P_count = ActiveSheet.PivotTables.Count
i = 1
Range("A2").Value = ListBox1.Value
MyRegion = Range("A2").Text
ProgressBar1.Visible = True
q = 0
x = 1
P_Loop = P_count * Sheets("Report").PivotTables("PivotTable" & i).PivotFields("Client Region").PivotItems.Count

Do
    With Sheets("Report").PivotTables("PivotTable" & i).PivotFields("Client Region")
        .PivotItems(MyRegion).Visible = True
        For Each Pi In .PivotItems
           If Pi.Name <> MyRegion Then Pi.Visible = False
           
           q = x / P_Loop * 100
           ProgressBar1.Value = q
           x = x + 1
         Next Pi
    End With
    i = i + 1
Loop Until i = P_count + 1
q = x / P_Loop * 100
End Sub
Private Sub UserForm_Initialize()
With ListBox1
    .AddItem "Central"
    .AddItem "Eastern Cape"
    .AddItem "Kwa-Zulu Natal"
    .AddItem "Limpopo"
    .AddItem "Mpumalanga"
    .AddItem "Northern Gauteng"
    .AddItem "Southern Gauteng"
    .AddItem "Western Cape"
End With
[COLOR="Red"]UNLOAD Me[/COLOR]
End Sub
 
Upvote 0
if you use

Code:
me.hide
before you turn off screenupdating, the form won't be visible

then use
Code:
unload me
after all your code is run. Nightcrawler has it in the 'initialise' event handler, which I'd expect to be problematic.
 
Last edited:
Upvote 0
Thanks guys,

hahah I was trying "unload.me"

thank you for your help. I have it all working now. :)
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top