【问题标题】:Looping ProgressBar Loader in VBA在 VBA 中循环 ProgressBar 加载器
【发布时间】:2017-08-15 03:05:17
【问题描述】:

我有一些 vba 代码需要一分钟左右才能在 excel 中运行,我试图添加进度条或某种类型的等待屏幕,以便更好地向用户显示代码仍在正常运行。我意识到 ProgressBar 只能显示特定类型代码的真实进度,我认为我的代码不会适用。

但是,我想知道是否有办法让 ProgressBar 循环,一旦它满了,它就会从头开始,直到我的代码完成;与插入电话时的充电图标类似。因此,在激活我的代码时,UserForm1 会弹出并显示一个重复的 ProgressBar,并且在我的代码完成后,UserForm 将卸载。

任何建议都会有所帮助。

谢谢!

【问题讨论】:

    标签: vba excel progress-bar


    【解决方案1】:

    我将以下内容用于用户表单上的进度条。它由一个框架内的标签组成;在开始时,标签的宽度为零,然后在 100% 完成时变宽以填充框架。该标签没有文本,但它具有设置为用作条的 BackColor 属性。框架右侧有第二个 progressText 标签。要调用此函数,请使用i = i + 1pctCompl = 100 * i / iTotalNum 之类的循环,然后在循环中包含:progress pctCompl, "listing files",完成后使用progress -1 使进度条不可见。您可以随时使用pctCompl = 0 将其重置为 0。

    Sub progress(pctCompl As Integer, Optional msg As String)
    Dim increment As Double, i As Integer
    If pctCompl < 0 Then
      If progressBar.Width < 0.9 * progressFrame.Width Then
        progressBar.Width = progressFrame.Width
        Application.Wait (Now + TimeValue("0:00:01"))
      End If
      progressText.Visible = False
      progressFrame.Visible = False
      Exit Sub
     Else
      progressText.Visible = True
      progressFrame.Visible = True
    End If
    progressText.Caption = pctCompl & "% " & msg
    progressBar.Width = progressFrame.Width * pctCompl / 100
    DoEvents
    End Sub
    
    Private Sub UserForm_Activate()
      progressFrame.Visible = False
      progressText.Visible = False
    end Sub
    

    【讨论】:

    • 代替等待,你可以做events,或者label.zorder 0,或者把label放在一个和frame.repaint一样大小的frame里。也使用双精度而不是整数,因为 vba 不会对舍入数字和最大值感到困惑(如果你没有得到它,请尝试 ?1000*1000 ,或者在即时窗口中,小数字但你会得到溢出错误)。为了避免代码变慢,例如,我经常使用 if i mod 3=0then affProgress ,但您可以更改值 3 以相应地调整它。
    猜你喜欢
    • 1970-01-01
    • 2015-01-12
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    相关资源
    最近更新 更多