• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Display processbar by label (wrong the value)

IceFrogBG

Member
Code:
Private Sub CommandButton5_Click()
Dim i, j, total, num1, num2, num3, num4 As Integer
Dim pct As Single
'For j = 1 To 2
total = 100
num1 = num2 = num3 = num4 = 25
pct = 0.01 * 100 / total
For i = 1 To num1
lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
'lblProg3.Caption = j & "%"
lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "#") & " %"
DoEvents
Next
For i = 1 To num2
lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "#") & " %"
'lblProg3.Caption = Format((i + 1) / fleCount * 100, "#") & " %"
DoEvents
Next
For i = 1 To num3
lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "#") & " %"
'lblProg3.Caption = Format((i + 1) / fleCount * 100, "#") & " %"
DoEvents
Next
For i = 1 To num4
lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "#") & " %"
'lblProg3.Caption = Format((i + 1) / fleCount * 100, "#") & " %"
DoEvents
Next
'Next
End Sub
Now I am trying to display the processbar (show how many percent complete)
But I have a trouble : some time when finished the result is 99%
So anyone can show me which point is not correct?
Thanks.
 
Do you realise that
Dim i, j, total, num1, num2, num3, num4 As Integer
means
Dim Num4 as Integer
all the rest are dimensioned as Variants
 
change CommandButton5_Click as shown below

Code:
Private Sub CommandButton5_Click()
Dim i As Integer, j As Integer
Dim total As Integer
Dim num1 As Integer, num2 As Integer, num3 As Integer, num4 As Integer
Dim pct As Double

'For j = 1 To 2
  total = 300
  num1 = 50
  num2 = 100
  num3 = 120
  num4 = 30
   
  pct = 0.01 * 100 / total
  For i = 1 To num1
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next
  For i = 0 To num2
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next
  For i = 0 To num3
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(Int(lblProg2.Width / lblProg1.Width * 100), "00") & " %"
  DoEvents
  Next
  For i = 0 To num4
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  Debug.Print i, lblProg2.Width, lblProg1.Width, lblProg2.Width / lblProg1.Width
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next
'Next
End Sub
 
Do you realise that
Dim i, j, total, num1, num2, num3, num4 As Integer
means
Dim Num4 as Integer
all the rest are dimensioned as Variants
Thanks Hui,
I think that when define
Dim a,b,c as Integer : all a and b and c as Integer.
 
Or try this code:

Code:
Sub Test_Var_Type()
'
' Refer to:
' https://support.office.com/en-us/article/VarType-Function-1e08636c-1892-40c2-aff3-2b894389e82d
'

Dim i, j, k As Long

MsgBox "Variable i is type: " & getvartype(VarType(i))
MsgBox "Variable j is type: " & getvartype(VarType(j))
MsgBox "Variable k is type: " & getvartype(VarType(k))

Dim x As Integer, y As String, z As Variant

MsgBox "Variable x is type: " & getvartype(VarType(x))
MsgBox "Variable y is type: " & getvartype(VarType(y))
MsgBox "Variable z is type: " & getvartype(VarType(z))


End Sub

Private Function getvartype(myVar As Integer) As String
Select Case myVar
  Case 0
  getvartype = "Empty"
  Case 1
  getvartype = "Null"
  Case 2
  getvartype = "Integer"
  Case 3
  getvartype = "Long Integer"
  Case 4
  getvartype = "Single-precision floating-point number"
  Case 5
  getvartype = "Double-precision floating-point number"
  Case 6
  getvartype = "Currency"
  Case 7
  getvartype = "Date"
  Case 8
  getvartype = "String"
  Case 9
  getvartype = "Object"
  Case 10
  getvartype = "Error"
  Case 11
  getvartype = "Boolean"
  Case 12
  getvartype = "Variant (Array)"
  Case 13
  getvartype = "Data Access Object"
  Case 14
  getvartype = "Decimal"
  Case 17
  getvartype = "Byte"
  Case 36
  getvartype = "Variant (user defined types)"
  Case 8192
  getvartype = "Array"
End Select

End Function
 
change CommandButton5_Click as shown below

Code:
Private Sub CommandButton5_Click()
Dim i As Integer, j As Integer
Dim total As Integer
Dim num1 As Integer, num2 As Integer, num3 As Integer, num4 As Integer
Dim pct As Double

'For j = 1 To 2
  total = 300
  num1 = 50
  num2 = 100
  num3 = 120
  num4 = 30
  
  pct = 0.01 * 100 / total
  For i = 1 To num1
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next
  For i = 0 To num2
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next
  For i = 0 To num3
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(Int(lblProg2.Width / lblProg1.Width * 100), "00") & " %"
  DoEvents
  Next
  For i = 0 To num4
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  Debug.Print i, lblProg2.Width, lblProg1.Width, lblProg2.Width / lblProg1.Width
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next
'Next
End Sub
Hello Hui,
T try your code and it work well.
Thanks so much.
But now I want to modify this code (use with the number (num1,num2,...num(n)) is not fix).
So can you help me modify code.
Ex : case 1 : num1+num2+num3 = total
click ProcessBar button : result is 100%,

case : num1+num2+num3 +...+ num(n)=total
click ProcessBar button : result is 100%,

If I use this code the result is 101%
 
Instead of Having Num1..Num(n) can't you simply use Num1 where Num1 = Total?
 
Instead of Having Num1..Num(n) can't you simply use Num1 where Num1 = Total?
Hello Hui,
Because I make this processbar to display the percent completed of count file
I have many folder (n folder),each folder have some file inside.
with folder1 have num1 file,
I want to do like that :
Code:
  For i = 1 To num1
  <code to do with file>
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next

other folder I will do same folder1.
after do all folder the processbar will display 100%.
Do you understand my explain.
Thanks.
 
Last edited by a moderator:
Pls try this:

Code:
Private Sub CommandButton5_Click()
Dim i As Integer, j As Integer
Dim total As Integer
Dim num1 As Integer, num2 As Integer, num3 As Integer, num4 As Integer
Dim pct As Double

'For j = 1 To 2
  num1 = 50
  num2 = 100
  num3 = 120
  num4 = 30
  total = num1 + num2 + num3 + num4
  
  pct = 1 / total

  For i = 1 To num1
  lblProg2.Width = lblProg2.Width + (pct * lblProg1.Width)
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next
  For i = num1 To num1 + num2
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next
  For i = num2 To num2 + num3
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(Int(lblProg2.Width / lblProg1.Width * 100), "00") & " %"
  DoEvents
  Next
  For i = num3 To num3 + num4
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next
'Next
End Sub
 
Pls try this:

Code:
Private Sub CommandButton5_Click()
Dim i As Integer, j As Integer
Dim total As Integer
Dim num1 As Integer, num2 As Integer, num3 As Integer, num4 As Integer
Dim pct As Double

'For j = 1 To 2
  num1 = 50
  num2 = 100
  num3 = 120
  num4 = 30
  total = num1 + num2 + num3 + num4
 
  pct = 1 / total

  For i = 1 To num1
  lblProg2.Width = lblProg2.Width + (pct * lblProg1.Width)
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next
  For i = num1 To num1 + num2
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next
  For i = num2 To num2 + num3
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(Int(lblProg2.Width / lblProg1.Width * 100), "00") & " %"
  DoEvents
  Next
  For i = num3 To num3 + num4
  lblProg2.Width = lblProg2.Width + pct * lblProg1.Width
  lblProg3.Caption = Format(lblProg2.Width / lblProg1.Width * 100, "00") & " %"
  DoEvents
  Next
'Next
End Sub
Hello Hui
I try your code and now it work well.
Thank you so much.
I will ask you if any trouble.
 
  • Like
Reactions: Hui
Back
Top