Excel 计算进度条百分比

Excel 计算进度条百分比,excel,vba,Excel,Vba,我知道这方面的帖子层出不穷,但由于我的数学技能是-100,我在计算正确的百分比时遇到了问题。下面是运行的循环,然后是试图计算百分比的子循环。问题在于标签的宽度是错误的,对于带有微小数字(如2)的记录集,所有的标签都被压扁了:-) 循环码 'loop until the end of the recordset Do While Not Glob_RecSet.EOF 'inner loop to get each record fields Fo

我知道这方面的帖子层出不穷,但由于我的数学技能是-100,我在计算正确的百分比时遇到了问题。下面是运行的循环,然后是试图计算百分比的子循环。问题在于标签的宽度是错误的,对于带有微小数字(如2)的记录集,所有的标签都被压扁了:-)

循环码

'loop until the end of the recordset
      Do While Not Glob_RecSet.EOF

         'inner loop to get each record fields
         For FieldCount = 0 To Glob_RecSet.Fields.Count - 1
            Glob_Sheet.Range(GLobWorkSheetRange).Offset(loopCounter, FieldCount).value = Glob_RecSet.Fields(FieldCount).value
         Next

            'start progress bar calculations the form show and unload is called on the form code apply changes button
            RunProgressBar loopCounter, TotalRows, "Runningquery for " & Glob_RecSetRunning

         'Next record
         Glob_RecSet.MoveNext

         'advance counter
         loopCounter = loopCounter + 1
      Loop
进度条的子代码

Public Sub RunProgressBar(loopCounter As Variant, TotalRecords As Variant, FormTitle As String)
   Dim LblDonewidth As Variant
   Dim ProgBarCaption As Variant
   Dim ProgresPercentage As Variant

   If (TotalRecords < 100) Then
      TotalRecords = 100
   End If

   ProgresPercentage = Round(loopCounter / TotalRecords * 100, 0)

  'to avoid to give the progress bar a percentage greater than 100
   If (ProgresPercentage > 100) Then
      ProgresPercentage = 100

   End If

   ProgBarCaption = Round(ProgresPercentage, 0) & "%"

   FrmProgBar.Caption = FormTitle
   FrmProgBar.LblDone.Width = ProgresPercentage * 2
   FrmProgBar.LblText.Caption = ProgBarCaption

   'The DoEvents statement is responsible for the form updating
   DoEvents
End Sub
公共子RunProgressBar(loopCounter作为变量,TotalRecords作为变量,FormTitle作为字符串)
Dim LBLDONEWITH作为变量
作为变体的标题
Dim PROGRESPENTAGE作为变量
如果(TotalRecords<100),则
TotalRecords=100
如果结束
ProgresPercentage=四舍五入(循环计数器/总记录*100,0)
'以避免进度条的百分比大于100
如果(进步百分比>100),则
进步百分比=100
如果结束
ProgBarCaption=四舍五入(ProgresPercentage,0)和“%”
FrmProgBar.Caption=FormTitle
FrmProgBar.LblDone.Width=程序百分比*2
FrmProgBar.LblText.Caption=ProgBarCaption
'DoEvents语句负责表单更新
多芬特
端接头

我找到了asnwer;主要的问题是我没有传递记录集中相应的记录总数;这可以通过在打开记录集之前添加下面的行来解决

'Clinet-Side cursor
Glob_RecSet.CursorLocation = adUseClient
然后我找到了这个例子,从中我获得了进度条百分比计算的正确逻辑。 在整个代码下面

Sub InitProgressBar(maxValue As Long)

    With FrmProgBar

        .LblDone.Tag = .LblRemain.Width / maxValue
        .LblDone.Width = 0
        .LblText.Caption = ""
    End With

End Sub

Public Sub RunProgressBar(loopCounter As Variant, formTitle As String)

   Dim LblDonewidth As Variant
   Dim ProgBarCaption As Variant
   Dim ProgresPercentage As Variant


   LblDonewidth = FrmProgBar.LblDone.Tag * loopCounter

   ProgresPercentage = Round(FrmProgBar.LblDone.Width / FrmProgBar.LblRemain.Width * 100, 0)

   ProgBarCaption = ProgresPercentage & "%"

  'to avoid to give the progress bar a percentage greater than 100
   If (ProgresPercentage > 100) Then
      ProgresPercentage = 100
   End If

   FrmProgBar.Caption = formTitle
   FrmProgBar.LblDone.Width = LblDonewidth
   FrmProgBar.LblText.Caption = ProgBarCaption

End Sub
它的用法如下

TotalRecords = Glob_RecSet.RecordCount

'init progressbar
InitProgressBar (TotalRecords)

 'loop until the end of the recordset
  Do While Not Glob_RecSet.EOF

      . . . . 
'The DoEvents statement is responsible for the form updating
 DoEvents

 'start progress bar calculations the form show and unload 
 'is called on the form code apply changes button
  RunProgressBar loopCounter, "Runningquery for " & Glob_RecSetRunning

这是否回答了您的问题我知道你已经确定了还有其他的问题,但这个问题有一些很好的例子,说明了如何在VBA中编码各种类型的进度指标(包括数学),我实际上找到了asnwer;除了在数学上完全是个哑巴,我还是VBA的新手,因为我是一名网络开发人员;我正在用我找到的答案编辑我的问题。请随意添加答案,而不是编辑你的问题。