Help with code

  • Thread starter Thread starter Jim May
  • Start date Start date
J

Jim May

Have tried below implementing "Displaying Progress% in Status Bar" as
recommended
in one of J Walkenbach's books ("How to" was a bit brief...) any way as
Macro RunABigOne runs the % remains unchanged at 100% << probably what I've
told it to do.
Can someone assist me in correcting to what is intended?
TIA,

Sub RunABigOne()
UpdateStatusBar 0
For x = 1 To 10000
Cells(x, "C").Value = "This is a test"
Next x
StopPC
End Sub

Sub UpdateStatusBar(PctDone)
Application.StatusBar = _
"Percent Completed: " & Format(PctDone, "100%")
End Sub

Sub StopPC()
Application.StatusBar = False
End Sub
 
Hi Jim,

Try:

Sub RunABigOne()
Dim x As Long

For x = 1 To 10000
UpdateStatusBar x / 10000
Cells(x, "C").Value = "This is a test"
Next x
StopPC
End Sub

Sub UpdateStatusBar(PctDone)
Application.StatusBar = _
"Percent Completed: " & Format(PctDone, "00%")
End Sub

Sub StopPC()
Application.StatusBar = False
End Sub

I've moved the call to UpdateStatusBar into the loop and used the x variable
in the argument. I've also amended the format.
 
Try
Application.StatusBar = "Percent Completed: " & Format(PctDone, "###%")
instead of
Application.StatusBar = "Percent Completed: " & Format(PctDone, "100%")

and add a line:
UpdateStatusBar x / 10000
within the for.. ..next loop thus:

Sub RunABigOne()
UpdateStatusBar 0
For x = 1 To 10000
Cells(x, "C").Value = "This is a test"
UpdateStatusBar x / 10000
Next x
StopPC
End Sub

and to make the status bar update a little less frequently (and to speed up
the macro):
If x / 1000 = Int(x / 1000) Then UpdateStatusBar x / 10000
instead of:
UpdateStatusBar x / 10000

Pascal
 
Thanks Norman;
I'll give it a go..
Jim

Norman Jones said:
Hi Jim,

Try:

Sub RunABigOne()
Dim x As Long

For x = 1 To 10000
UpdateStatusBar x / 10000
Cells(x, "C").Value = "This is a test"
Next x
StopPC
End Sub

Sub UpdateStatusBar(PctDone)
Application.StatusBar = _
"Percent Completed: " & Format(PctDone, "00%")
End Sub

Sub StopPC()
Application.StatusBar = False
End Sub

I've moved the call to UpdateStatusBar into the loop and used the x variable
in the argument. I've also amended the format.
 
Pascal, thanks;
I'll check it out.
Jim

P Daulton said:
Try
Application.StatusBar = "Percent Completed: " & Format(PctDone, "###%")
instead of
Application.StatusBar = "Percent Completed: " & Format(PctDone, "100%")

and add a line:
UpdateStatusBar x / 10000
within the for.. ..next loop thus:

Sub RunABigOne()
UpdateStatusBar 0
For x = 1 To 10000
Cells(x, "C").Value = "This is a test"
UpdateStatusBar x / 10000
Next x
StopPC
End Sub

and to make the status bar update a little less frequently (and to speed up
the macro):
If x / 1000 = Int(x / 1000) Then UpdateStatusBar x / 10000
instead of:
UpdateStatusBar x / 10000

Pascal
 
Jim,

Perennial problem of progress bars is knowing what point in the loop you
are. Your example is very simple because you have a very fixed boundary,
100000, so it is easy to calculate the position, but in real world examples
it is not so clear cut. As the objective is to show something is happening,
it is the visual feedback that is important. Because of this, what I tend to
do is call a progress bar routine many times that goes from 0% to 100%
relatively quickly, doing this from several points that are
'self-contained'. That way, you provide feedback regularly, you even get
away with no progress showing when moving from one code section to another,
and coding is more manageable. There are still elements of trial and error
in the code, but less fine tuning.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Thanks Bob for the added info...
Jim

Bob Phillips said:
Jim,

Perennial problem of progress bars is knowing what point in the loop you
are. Your example is very simple because you have a very fixed boundary,
100000, so it is easy to calculate the position, but in real world examples
it is not so clear cut. As the objective is to show something is happening,
it is the visual feedback that is important. Because of this, what I tend to
do is call a progress bar routine many times that goes from 0% to 100%
relatively quickly, doing this from several points that are
'self-contained'. That way, you provide feedback regularly, you even get
away with no progress showing when moving from one code section to another,
and coding is more manageable. There are still elements of trial and error
in the code, but less fine tuning.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top