How to use ProgressBar in VBA

How to use ProgressBar in VBA

Anonymous
Not applicable
4,412 Views
10 Replies
Message 1 of 11

How to use ProgressBar in VBA

Anonymous
Not applicable
How to use ProgressBar and StatusBar in VBA?
Thanks:)
0 Likes
4,413 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable
very carefully
0 Likes
Message 3 of 11

Anonymous
Not applicable
Can you give an example?
0 Likes
Message 4 of 11

Anonymous
Not applicable
' drop a progress bar on a userform, drop in this code, and click the form to activate...

Private Sub UserForm_Click()
' start progress Control
Dim MaxInt As Integer: MaxInt = 32650

ProgressBar1.Min = 0: ProgressBar1.Max = MaxInt
For I% = 1 To MaxInt
For J% = 1 To MaxInt
Next J%
ProgressBar1.Value = I%
Next I%

End Sub


' that is the simplest example. Progress bars are normally used when running long sections of code; you have to find where to sprinkle in your own calls to the progress bar to update it, depending on your code
0 Likes
Message 5 of 11

Anonymous
Not applicable
' drop a progress bar on a userform, drop in this code, and click the form to activate...

Private Sub UserForm_Click()
' start progress Control
Dim MaxInt As Integer: MaxInt = 32650

ProgressBar1.Min = 0: ProgressBar1.Max = MaxInt
For I% = 1 To MaxInt
For J% = 1 To MaxInt
Next J%
ProgressBar1.Value = I%
Next I%

End Sub


' that is the simplest example. Progress bars are normally used when running long sections of code; you have to find where to sprinkle in your own calls to the progress bar to update it, depending on your code
0 Likes
Message 6 of 11

Anonymous
Not applicable
To:rocheey
Thanks
I want to do it within CAD,not use UserForm.
0 Likes
Message 7 of 11

Anonymous
Not applicable
>>Subject:Re: How to use ProgressBar in VBA
>> I want to do it within CAD,not use UserForm.

huh?

VBA, but not userform?

in Cad?

I think you might want to rephrase your question.

if you're referring to the code-only class, done thru the API, Ive had issues with that - like issues with it not disappearing from the screen.

Otherwise, I have absolutely NO CLUE what you're talking about.
0 Likes
Message 8 of 11

ajaafreh
Observer
Observer

"luojiandan" mean to do it within VBA for app on AutoCAD VBA manager ,not on VB Program User Form Mr.huh?
0 Likes
Message 9 of 11

ajaafreh
Observer
Observer
Here's a quick tip for making a VBA progress bar without adding a reference to the MSComCtl.ocx control, or any other custom control.

Add a Textbox to your form
Position and size it to it's maximum size.
Make note of the width of the textbox when you are done.

Add the following code to your form Initialize event.


TextBox1.Width = 0
TextBox1.BackColor = vbBlue '(or the color of your choice)



Now in the procedure for which you want to show progress, follow this:

Divide the width of the textbox by the number of iterations, this is the amount by which the progress bar will grow for each iteration.

Then in your iteration loop, increment the width of the textbox by the above result.

Here is an example.


Private Sub CommandButton1_Click()
Dim i As Integer, x As Double
Dim num As Integer, start As Double
num = 150 '(this will typically be a variable in your procedure)
x = 206 / num
For i = 1 To num
'-------------------------------
' this dummy code is just to give
' the progress bar time to work
' in this example
start = Timer
Do While Timer < start + 0.02
DoEvents
Loop
' end dummy code
'-------------------------------
' This changes the progress bar
TextBox1.Width = i * x
Next i
End Sub



num will typically already be in your procedure as the number of items in a collection or array that you are processing.

x is the width of the textbox divided by num

For i = 1 to num is just a sample loop. You will have your own.

Setting the textbox width to i * x at each iteration is what makes the "Progress Bar" actually work as shown below.
0 Likes
Message 10 of 11

Anonymous
Not applicable

Just reading your comment a decade later (hah!), but I think OP is trying to use something similar to Excel's application.statusbar command, which controls the little notification bar at the bottom of the Excel window. Him and myself both wish CAD had this option to edit in VBA, but it doesn't seem like you can do so easily. 

0 Likes
Message 11 of 11

DORICID3Y5ZV
Community Visitor
Community Visitor
PRIVATE VL AS OBJECT

PRIVATE VLF AS OBJECT

Function OPENCAD() AS BOOLEAN
OPENCAD = False
On Error GoTo erro
Dim cf

Dim ZS As Integer
Dim ZS1P As Integer
cont = 19
Set Cad = GetObject(, "autocad.application." & cont)
GoTo aki
abri:
cont = 19
Set Cad = CreateObject("autocad.application." & cont)
Cad.Visible = True
OPENCAD = True
aki:
On Error GoTo 0
Set Doc = Cad.ActiveDocument

Set VL = Doc.Application.GetInterfaceObject("VL.Application.16")
Set VLF = VL.ActiveDocument.Functions

Exit Function
erro:
ER = Err
vARCANCEL = Doc.GetVariable("LASTPROMPT")
If UCase(Right(vARCANCEL, 8)) = "*CANCEL*" Then End
If ER = 440 Then Resume aki
If ER = 429 Then
cont = cont + 1
If cont > 50 And TE = 0 Then
TE = 1
Resume abri
End If
Resume
End If
MsgBox "ERRO - " & Err.Description
End Function


'IN CODIGO

'VISIBLE PROGRESS CAD
ZS = 100 ' COUNT ITEMS
VL.ActiveDocument.Functions.ITEM("acet-ui-progress-init").FUNCALL "Working:", ZS
ZS1P = Int(ZS / 50)
If ZS1P < 50 Then ZS1P = 1

For ZS = 0 To 100 ' COUNT ITEMS
  'REFRESH PROGRESS
  If ZS / ZS1P = Int(ZS / ZS1P) Then 
  VLF.ITEM("acet-ui-progress-safe").FUNCALL (ZS)
End If
NEXT ZS
'INVIBLE PROGRESS CAD
VLF.ITEM("acet-ui-progress-done").FUNCALL
0 Likes