【问题标题】:Creating a progress bar in Excel VBA [closed]在 Excel VBA 中创建进度条 [关闭]
【发布时间】:2012-07-16 19:48:08
【问题描述】:

我找到了一个 Excel VBA 脚本和“教程”,但它并没有真正破坏代码,而是将条形码分成单独的部分。

http://spreadsheetpage.com/index.php/tip/displaying_a_progress_indicator/

“演示”附带的脚本会在进度条穿过时将随机数添加到 Excel 工作表中。

工作表上的这段代码没有做的是将部分分解,所以说'这是随机数的代码,'这是实际进度条的代码。

有人可以剖析这段代码,让它对那些不会说 VBAeese 的人以及那些似乎会写它的人更“用户友好”吗?

提前致谢。

【问题讨论】:

  • 是的,也不是……除了在自动填充随机信息的地方写它,它会根据脚本所处的步骤而改变。

标签: excel vba


【解决方案1】:

这里是给你的代码的注释版本:

Sub Main()
'   Inserts random numbers on the active worksheet
    Dim Counter As Integer
    Dim RowMax As Integer, ColMax As Integer
    Dim r As Integer, c As Integer
    Dim PctDone As Single


    If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
    '- if this subroutine is ran on a sheet that is not called "Worksheet" then exit
    '-- change 'Worksheet' to whatever sheet you want the progress bar on

    Cells.Clear
    '- clear all cells in active worksheet

    Application.ScreenUpdating = False
    '-disables updating the screen in the for loop that follows
    '- that way if we are editing 1000 cells int he workbook it only needs to update them at the end when
    '- this is set back to true

    Counter = 1 '- counter counts what cell we are on
    RowMax = 100 '- this is how many rows will be filled with data
    ColMax = 25 '- this is how many columns will be filled with data

    '- note that Rowmax * ColMax = 2,500 co counter will go from 1 to 2,500

    For r = 1 To RowMax
        '-for each row 1 to 100 we will loop through the 25 columns to add the random number
        For c = 1 To ColMax
            '- enter a random number into the cell we are on (Cells(r,c))
            Cells(r, c) = Int(Rnd * 1000)
            '- +1 to the coutner so we can count which cell we ar eon out of 2,500
            Counter = Counter + 1
        Next c
        '- after finishing each column but before starting the next row
        '- check what percent done we are (to update the userform)
        PctDone = Counter / (RowMax * ColMax)

        '- Edit the progressbar called "UserForm1" (already exists in workbook)
        With UserForm1
            'Userform has 2 items in it a Label called 'FrameProgress' and a onject called 'LabelProgress'
            'Change the text in the Label called 'FrameProgress' to display the percent done we calculated earlier
            .FrameProgress.Caption = Format(PctDone, "0%")
            ' Resize the object called 'LabelProgress' to be X perxent of the width of the previous label (minus 10 to leave room on the edge)
            ' - where X is the percent we are done
            .LabelProgress.Width = PctDone * (.FrameProgress.Width - 10)
        End With
'       The DoEvents statement is responsible for the form updating
        DoEvents
    Next r
    '- exit form when it is at 100%
    Unload UserForm1
End Sub

对您有用的代码的唯一部分是注意,当它循环时,它会计算完成的百分比,然后使用它来更新表单。

如果您有很多代码,您可以简单地将以下内容放在整个代码中(假设您构建了表单)

Sub Example()
    'wait a few seconds
    Application.Wait (100000)
    'your code goes here instead of .wait

    PctDone = 0.3
    With UserForm1
        'Userform has 2 items in it a Label called 'FrameProgress' and a onject called 'LabelProgress'
        'Change the text in the Label called 'FrameProgress' to display the percent done we calculated earlier
        .FrameProgress.Caption = Format(PctDone, "0%")
        ' Resize the object called 'LabelProgress' to be X perxent of the width of the previous label (minus 10 to leave room on the edge)
        ' - where X is the percent we are done
        .LabelProgress.Width = PctDone * (.FrameProgress.Width - 10)
    End With
    'The DoEvents statement is responsible for the form updating
    DoEvents

    Application.Wait (100000)
    'your code goes here instead of .wait

    PctDone = 0.6
    With UserForm1
        .FrameProgress.Caption = Format(PctDone, "0%")
        .LabelProgress.Width = PctDone * (.FrameProgress.Width - 10)
    End With
    DoEvents

    Application.Wait (100000)
    'your code goes here instead of .wait

    PctDone = 0.9
    With UserForm1
        .FrameProgress.Caption = Format(PctDone, "0%")
        .LabelProgress.Width = PctDone * (.FrameProgress.Width - 10)
    End With
    DoEvents

    Application.Wait (100000)
    'your code goes here instead of .wait

End Sub

【讨论】:

  • 好的,阅读你所说的这个循环是否正确?所以如果我想做到这一点,如果每个 With XXX 你会如何让它工作?所以它会显示 With XXX1 = 10%, With XXX2 = 20% 等...因为它看起来像这里的写法,它考虑了脚本在填充单元格的位置...我错了吗?
  • 更新的答案包括一个伪代码的虚拟示例,说明如何在原始 for 循环之外实现 % 框。
  • 我相信这正是我正在寻找的东西,非常感谢,我会测试一下,如果它有效或无效,我明天会回复,但我希望它会。
  • + 1 很好地评论和解释:)
  • 请注意,您将不得不制作它正在谈论的用户表单。它的关键方面只是将该框的宽度调整为不同的 %'ges
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多