Progress bar

R

ripamonti

Hi to everyone,
I m trying to use this samle progress bar with my code(it is a
montecarlo simulation programme). I don't really know how to connect
this program to my loop code(montecarlo).
do you have some suggestions?


Sub ProgressBarSimpleDemo2()
Dim sb As clsProgressBar, i As Long
Dim Counter As Long
Dim NumRows As Long, NumCols As Integer
Dim r As Long, c As Integer
Dim Total As Long

Set sb = New clsProgressBar ' create a new progress bar
With sb
.Show "Entering random numbers", vbNullString, 0 ' display the
progress bar
.ProgressBarFG_RGB = RGB(255, 0, 0) 'set foreground to red
End With

' Generate random numbers
Range("A2:IV65536").Clear
Counter = 0
NumRows = 1000
NumCols = 50
Total = NumRows * NumCols
For r = 1 To NumRows
For c = 1 To NumCols
Counter = Counter + 1
Cells(r + 1, c) = Int(Rnd() * 500)
' update the progress bar?
If Counter Mod 50 = 0 Then sb.PercentComplete = (Counter /
Total) * 100
Next c
Next r
Set sb = Nothing ' remove the progress bar
MsgBox Total & " random numbers were generated."
Range("A2:IV65536").Clear
End Sub
 
B

Bob Phillips

As you can see, the progress bar is updated by the code, calls are made from
within the code to it

' update the progress bar?
If Counter Mod 50 = 0 Then sb.PercentComplete = (Counter /
Total) * 100

You need to establish similar points in your code where you can make calls
to the PB. The tricky parts are knowing how much work is to be done, and how
much has been done. A For n = 1 To Limit ... Next n loop is easy, you know
that it will loop Limit times, and the current count is in n. Other code may
need different approaches. I often find that I create a PB that will quickly
get to say 30%, take ages to get to 40%, quick to 60, etc., because I do not
know in advance what times will be taken, but this is still preferable to
just seeing the hourglass.


--
---
HTH

Bob

(change the xxxx to gmail 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

Top