Forum Discussion
Progressbar in Excel (VBA)
Maybe I'm trying to do the impossible. Who knows ?
I've made a vba uerform to do several things (too long to explain) but what I would like to do is while the progressbar is running to have a small text appear from under the (now) dark blue progress.
So it runs from left to right, at a certain point I would like some text to appear letter by letter as the bar gets longer.
I tried right clicking on the progress bar and moved it to the back and moved the text to the front. Result is that text stays behing progress bar.
Hope this is clear, and hope to see if anyone comes up with a solution.
Thanks in advance, Andy
3 Replies
- AndyPeCopper Contributor
Thanks Nikolino but I'm using Office For home and students 2021.
Maybe that is why I cannot find 'progresstext' or 'progresstext.caption' under the progress bar.
Fyi I created this as a test to see if your suggestion works for me
The form the progress bar shows is named PB, the progress bar is named PBAR
Looking at your code I think I should use this for example:
Sub StartProgress()
Dim i As Integer
Dim progress As Integer
Dim fullText As String
fullText = "Processing..."
PB1.PBar.Width = 0 ' Initially empty
PB1.PBar.Caption = "" ' Initially empty text STOPS HERE !!!! <<<<<<etc,etc,
Here you can see what is available for coding PBAR, 'progresstext' or 'progresstext.caption'
Thanks again, does this help any ?
Regards, Andy
- NikolinoDEGold Contributor
Thanks for the screenshot — it confirms that you're using a standard ProgressBar control (likely from the Microsoft Windows Common Controls library), which doesn't support displaying text directly over or inside the bar.
To achieve your animation effect, you need to use two separate controls:
ProgressBar (PBar) – for the visual progress
Label (ProgressText) – placed above the ProgressBar, to show the text
Add a Label to your form.
- Name it ProgressText
- Make its BackStyle = Transparent (or 0 - fmBackStyleTransparent)
- Place it over your PBar control in the UserForm
Update your code to use this ProgressText Label:
Private Sub UserForm_Initialize() ' Reset ProgressBar and Label Me.PBar.Width = 0 Me.ProgressText.Caption = "" End Sub Sub StartProgress() Dim i As Integer Dim progress As Integer Dim fullText As String fullText = "Processing..." For i = 1 To 100 ' Simulate progress bar width growth progress = i * 1.5 ' Adjust as needed Me.PBar.Width = progress ' Show text letter by letter If i <= Len(fullText) Then Me.ProgressText.Caption = Mid(fullText, 1, i) End If ' Make sure label stays in front Me.ProgressText.ZOrder 0 ' 0 = bring to front DoEvents Application.Wait Now + TimeValue("00:00:00.03") Next i End Sub
The .Caption property is only available on controls like Label, CommandButton, etc. Not ProgressBar.
If your Label disappears behind the progress bar, it might be because ProgressBar is a system control that renders on top of everything. If that's the case:
- Use a frame and custom drawing instead of the standard progress bar.
- Or, simulate the progress bar with a shape (another label or image box).
Hope this helps you :-)
- NikolinoDEGold Contributor
Private Sub UserForm_Initialize() ' Initial Setup for the Progress Bar and Text Me.ProgressBar.Width = 0 ' Initially empty Me.ProgressText.Caption = "" ' Initially empty text End Sub Sub StartProgress() Dim i As Integer Dim progress As Integer Dim fullText As String fullText = "Processing..." ' Start Progress Loop For i = 1 To 100 ' Update Progress Bar width progress = i * 2 ' Adjust multiplier to fit your form size Me.ProgressBar.Width = progress ' Reveal text letter by letter If i <= Len(fullText) Then Me.ProgressText.Caption = Mid(fullText, 1, i) End If ' Bring the text to the front each time it updates (ensures text stays visible) Me.ProgressText.ZOrder msoSendToFront ' Small Delay for Animation Effect DoEvents Application.Wait Now + TimeValue("00:00:00.05") ' Adjust speed of progress Next i End Sub
In theory, this should solve your problem of displaying the text letter by letter while animating the progress bar. The key is to ensure the text control is always on top using .ZOrder, and that both the progress bar and the text are dynamically updated within the loop. Otherwise, insert a file into the theme.
Hope this helps.