【问题标题】:Excel VBA Progress Bar with userform带有用户窗体的 Excel VBA 进度条
【发布时间】:2020-08-18 13:02:11
【问题描述】:

我正在尝试学习如何在用户窗体中使用进度条。

我的代码的问题是它在运行循环后显示进度条;它应该在显示进度条的同时运行循环,而不是像 10%...20%...30%...40%......100%。

谁能帮我修复我的代码来实现这个目标?

'-----Below is loop-----------------------------------------------------
Sub looprange()
   Dim r As Range
'----------loop thru 0 to 9 ---------------
   For Each r In Sheet6.Range("j2", Range("j" & Rows.Count).End(xlUp))

      Sheet6.Range("i2").Value = r.Value

      ActiveWindow.ScrollRow = 11
      Application.CutCopyMode = False

      Call print_jpeg

   Next r

   MsgBox "done"

End Sub

--

'--------Below is vba code in userform :------------
Private Sub UserForm_Activate()

    Dim remainder As Long
    Dim i As Long, j As Long

    Call looprange

    remainder = 0

    For i = 1 To 200

        UserForm1.Label2.Width = UserForm1.Label2.Width + 1

        If i Mod 2 = 0 Then
            remainder = remainder + 1
            UserForm1.Caption = remainder & ” % complete”
            UserForm1.Label2.Caption = remainder & “%”
        End If

        For j = 1 To 600

            DoEvents

        Next j

    Next i

    MsgBox “Loading of program complete.”

    Unload UserForm1

End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我相信标准 VBA 中不包含真正的状态栏(没有引用),但是您可以滥用标签控件。请注意,此方法以性能为代价换取用户的清晰度(用户可以看到应用程序正在运行,但它比没有状态栏的同一个应用程序慢)

    只需添加三个标签,一个用于状态文本,一个用于实际移动栏,一个用于全栏边框。并格式化它们以适合您的应用程序(见下图):

    代码如下:

    Private Sub cbStart_Click()
     one_percent_bar_width = lLoadBar.Width / 100 'width of one percent of the total loading bar
     max_numbers = 1000 'only for demo purpose
    
     Me.lLoadingBar.Visible = True
     For i = 0 To max_numbers 'your loop here
    
         'your code here
    
       percentage = i / max_numbers * 100 'calculation of progress, replace i and max_numbers to fit your loop
       Me.lStatus.Caption = percentage & "% complete" 'status percentage text
       Me.lLoadingBar.Width = percentage * one_percent_bar_width 'width of actual blue bar
       DoEvents 'slows down code but only way to make the bar visibly move, tradeoff between user clarity and speed
     Next i 'edit to fit your loop
     Me.lStatus.Caption = "Complete!" 'adjust status to whatever you want
    End Sub
    
    Private Sub UserForm_Initialize()
     Me.lLoadingBar.Visible = False 'hide the small blue bar
     Me.lStatus.Caption = "Progress not started" 'adjust status to whatever you want
    End Sub
    

    【讨论】:

    • 我的代码现在可以运行了,非常感谢您的分享!
    【解决方案2】:

    您的代码存在一些问题,但为了专注于进度条部分,我将分享一个在 Excel 中处理进度条的方法的示例(使用内置状态栏)。

    这个例子实际上并没有做任何有用的事情,而是在循环之间暂停了片刻,但在状态栏上报告了状态。)希望它会给你一些想法。

    Sub ProgressBarTest()
    
        Const LoopsToRun = 500
        Dim z As Integer
    
        For z = 1 To LoopsToRun
            'put a random number in A1
            Range("A1") = Int(Rnd() * 100) + 1
    
            'update status bar
            Application.StatusBar = "Progress: " & Format((z / LoopsToRun), "0.0%")
    
            'pause for .3 seconds (instead of pausing, you'd run your actual procedure here)
            Pause (0.1)
        Next z
    
        Application.StatusBar = "Complete!"
    
    End Sub
    
    Sub Pause(sec As Single)
        'pauses for [sec] second
        Dim startTime As Single
        startTime = Timer
        Do While Timer < startTime + sec
            DoEvents
        Loop
    End Sub
    

    更多信息herehere

    【讨论】:

    • 感谢您的帮助,我可以使用用户表单来实现吗?不是excel底部的状态栏。
    【解决方案3】:

    VBA 有一个可以添加到表单的进度条控件。在学习的过程中,您可以简单地将这个控件添加到表单中,然后在执行有用表单功能的循环期间更新该控件。进度条控件包括有用的属性,例如最小值和最大值。

    如果您在表单中执行多项操作,您可以更新一个标签以告诉用户正在进行什么以及进度量。

    [更高级] 在我之前的一些工作中,我设置了 VBA 例程在后台运行,并使用事件创建了一个进度条表单。这允许使用进度语句和百分比运行来获得更复杂的视图。但这种形式的基础仍然是进度条控件。虽然使用事件更复杂,但它允许我制作一个更通用的进度条表单,可以由我的任何 vba 函数使用,并且这些函数不受我对表单所做的任何更改的影响,因为事件充当一种标准界面。

    来自@MathieuGuindon 的评论更新:“该控件实际上是一个 VB6 控件,并且只能在 .OCX 正确注册的 32 位 VBA 主机中工作,而且越来越多使用较新的 Windows 版本具有挑战性;强烈建议不要在 VBA7 中引用和使用这些控件。”

    【讨论】:

    • 该控件实际上是一个VB6控件,并且只能在正确注册了.OCX的32位VBA主机中工作,这对于最近的Windows版本变得越来越具有挑战性;强烈建议不要在 VBA7 中引用和使用这些控件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 2013-02-15
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多