简体   繁体   English

如何在 Excel VBA 中创建进度条?

[英]How can I create a progress bar in Excel VBA?

I'm doing an Excel app that needs a lot data updating from a database, so it takes time.我正在做一个需要从数据库更新大量数据的 Excel 应用程序,因此需要时间。 I want to make a progress bar in a userform and it pops up when the data is updating.我想在用户窗体中制作一个进度条,并在数据更新时弹出。 The bar I want is just a little blue bar moves right and left and repeats till the update is done, no percentage needed.我想要的条只是一个蓝色的小条,左右移动并重复直到更新完成,不需要百分比。

I know I should use the progressbar control, but I tried for sometime, but can't make it.我知道我应该使用progressbar控件,但我尝试了一段时间,但无法做到。

My problem is with the progressbar control, I can't see the bar 'progress'.我的问题是progressbar条控件,我看不到进度条。 It just completes when the form pops up.它只是在表单弹出时完成。 I use a loop and DoEvent but that isn't working.我使用循环和DoEvent但这不起作用。 Plus, I want the process to run repeatedly, not just one time.另外,我希望该过程重复运行,而不仅仅是一次。

Sometimes a simple message in the status bar is enough:有时状态栏中的一条简单消息就足够了:

使用 VBA 在 Excel 状态栏中显示消息

This is very simple to implement :很容易实现

Dim x               As Integer 
Dim MyTimer         As Double 

'Change this loop as needed.
For x = 1 To 50
    ' Do stuff
    Application.StatusBar = "Progress: " & x & " of 50: " & Format(x / 50, "0%")
Next x 

Application.StatusBar = False

Here's another example using the StatusBar as a progress bar.这是使用 StatusBar 作为进度条的另一个示例。

By using some Unicode Characters, you can mimic a progress bar.通过使用一些 Unicode 字符,您可以模仿进度条。 9608 - 9615 are the codes I tried for the bars. 9608 - 9615 是我为条形尝试的代码。 Just select one according to how much space you want to show between the bars.只需根据要在条形之间显示多少空间来选择一个。 You can set the length of the bar by changing NUM_BARS.您可以通过更改 NUM_BARS 来设置条的长度。 Also by using a class, you can set it up to handle initializing and releasing the StatusBar automatically.此外,通过使用类,您可以将其设置为自动处理初始化和释放状态栏。 Once the object goes out of scope it will automatically clean up and release the StatusBar back to Excel.一旦对象超出范围,它将自动清理并将状态栏释放回 Excel。

' Class Module - ProgressBar
Option Explicit

Private statusBarState As Boolean
Private enableEventsState As Boolean
Private screenUpdatingState As Boolean
Private Const NUM_BARS As Integer = 50
Private Const MAX_LENGTH As Integer = 255
Private BAR_CHAR As String
Private SPACE_CHAR As String

Private Sub Class_Initialize()
    ' Save the state of the variables to change
    statusBarState = Application.DisplayStatusBar
    enableEventsState = Application.EnableEvents
    screenUpdatingState = Application.ScreenUpdating
    ' set the progress bar chars (should be equal size)
    BAR_CHAR = ChrW(9608)
    SPACE_CHAR = ChrW(9620)
    ' Set the desired state
    Application.DisplayStatusBar = True
    Application.ScreenUpdating = False
    Application.EnableEvents = False
End Sub

Private Sub Class_Terminate()
    ' Restore settings
    Application.DisplayStatusBar = statusBarState
    Application.ScreenUpdating = screenUpdatingState
    Application.EnableEvents = enableEventsState
    Application.StatusBar = False
End Sub

Public Sub Update(ByVal Value As Long, _
                  Optional ByVal MaxValue As Long= 0, _
                  Optional ByVal Status As String = "", _
                  Optional ByVal DisplayPercent As Boolean = True)

    ' Value          : 0 to 100 (if no max is set)
    ' Value          : >=0 (if max is set)
    ' MaxValue       : >= 0
    ' Status         : optional message to display for user
    ' DisplayPercent : Display the percent complete after the status bar

    ' <Status> <Progress Bar> <Percent Complete>

    ' Validate entries
    If Value < 0 Or MaxValue < 0 Or (Value > 100 And MaxValue = 0) Then Exit Sub

    ' If the maximum is set then adjust value to be in the range 0 to 100
    If MaxValue > 0 Then Value = WorksheetFunction.RoundUp((Value * 100) / MaxValue, 0)

    ' Message to set the status bar to
    Dim display As String
    display = Status & "  "

    ' Set bars
    display = display & String(Int(Value / (100 / NUM_BARS)), BAR_CHAR)
    ' set spaces
    display = display & String(NUM_BARS - Int(Value / (100 / NUM_BARS)), SPACE_CHAR)

    ' Closing character to show end of the bar
    display = display & BAR_CHAR

    If DisplayPercent = True Then display = display & "  (" & Value & "%)  "

    ' chop off to the maximum length if necessary
    If Len(display) > MAX_LENGTH Then display = Right(display, MAX_LENGTH)

    Application.StatusBar = display
End Sub

Sample Usage:示例用法:

Dim progressBar As New ProgressBar

For i = 1 To 100
    Call progressBar.Update(i, 100, "My Message Here", True)
    Application.Wait (Now + TimeValue("0:00:01"))
Next

In the past, with VBA projects, I've used a label control with the background colored and adjust the size based on the progress.过去,在 VBA 项目中,我使用带有背景颜色的标签控件并根据进度调整大小。 Some examples with similar approaches can be found in the following links:可以在以下链接中找到一些具有类似方法的示例:

  1. http://oreilly.com/pub/h/2607 http://oreilly.com/pub/h/2607
  2. http://www.ehow.com/how_7764247_create-progress-bar-vba.htmlhttp://www.ehow.com/how_7764247_create-progress-bar-vba.html
  3. http://spreadsheetpage.com/index.php/tip/displaying_a_progress_indicator/ http://spreadsheetpage.com/index.php/tip/displaying_a_progress_indicator/

Here is one that uses Excel's Autoshapes:这是使用 Excel 的 Autoshapes 的一种:

http://www.andypope.info/vba/pmeter.htm http://www.andypope.info/vba/pmeter.htm

I'm loving all the solutions posted here, but I solved this using Conditional Formatting as a percentage-based Data Bar.我喜欢这里发布的所有解决方案,但我使用条件格式作为基于百分比的数据栏解决了这个问题。

条件格式

This is applied to a row of cells as shown below.这适用于一行单元格,如下所示。 The cells that include 0% and 100% are normally hidden, because they're just there to give the "ScanProgress" named range (Left) context.包含 0% 和 100% 的单元格通常是隐藏的,因为它们只是为了提供“ScanProgress”命名范围(左)上下文。

扫描进度

In the code I'm looping through a table doing some stuff.在代码中,我在一个表中循环做一些事情。

For intRow = 1 To shData.Range("tblData").Rows.Count

    shData.Range("ScanProgress").Value = intRow / shData.Range("tblData").Rows.Count
    DoEvents

    ' Other processing

Next intRow

Minimal code, looks decent.最少的代码,看起来不错。

============== This code goes in Module1 ============

Sub ShowProgress()
    UserForm1.Show
End Sub

============== Module1 Code Block End =============

Create a Button on a Worksheet;在工作表上创建一个按钮; map button to "ShowProgress" macro将按钮映射到“ShowProgress”宏

Create a UserForm1 with 2 buttons, progress bar, bar box, text box:创建一个带有 2 个按钮、进度条、条形框、文本框的 UserForm1:

UserForm1 = canvas to hold other 5 elements
CommandButton2 = Run Progress Bar Code; Caption:Run
CommandButton1 = Close UserForm1; Caption:Close
Bar1 (label) = Progress bar graphic; BackColor:Blue
BarBox (label) = Empty box to frame Progress Bar; BackColor:White
Counter (label) = Display the integers used to drive the progress bar

======== Attach the following code to UserForm1 =========

Option Explicit

' This is used to create a delay to prevent memory overflow
' remove after software testing is complete

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub UserForm_Initialize()

    Bar1.Tag = Bar1.Width
    Bar1.Width = 0

End Sub
Sub ProgressBarDemo()
    Dim intIndex As Integer
    Dim sngPercent As Single
    Dim intMax As Integer
    '==============================================
    '====== Bar Length Calculation Start ==========

    '-----------------------------------------------'
    ' This section is where you can use your own    '
    ' variables to increase bar length.             '
    ' Set intMax to your total number of passes     '
    ' to match bar length to code progress.         '
    ' This sample code automatically runs 1 to 100  '
    '-----------------------------------------------'
    intMax = 100
    For intIndex = 1 To intMax
        sngPercent = intIndex / intMax
        Bar1.Width = Int(Bar1.Tag * sngPercent)
        Counter.Caption = intIndex


    '======= Bar Length Calculation End ===========
    '==============================================


DoEvents
        '------------------------
        ' Your production code would go here and cycle
        ' back to pass through the bar length calculation
        ' increasing the bar length on each pass.
        '------------------------

'this is a delay to keep the loop from overrunning memory
'remove after testing is complete
        Sleep 10

    Next

End Sub
Private Sub CommandButton1_Click() 'CLOSE button

Unload Me

End Sub
Private Sub CommandButton2_Click() 'RUN button

        ProgressBarDemo

End Sub

================= UserForm1 Code Block End =====================

============== This code goes in Module1 =============

Sub ShowProgress()
    UserForm1.Show
End Sub

============== Module1 Code Block End =============

I liked the Status Bar from this page:我喜欢这个页面的状态栏:

https://wellsr.com/vba/2017/excel/vba-application-statusbar-to-mark-progress/ https://wellsr.com/vba/2017/excel/vba-application-statusbar-to-mark-progress/

I updated it so it could be used as a called procedure.我更新了它,以便它可以用作被调用的过程。 No credit to me.对我没有信用。


showStatus Current, Total, "  Process Running: "

Private Sub showStatus(Current As Integer, lastrow As Integer, Topic As String)
Dim NumberOfBars As Integer
Dim pctDone As Integer

NumberOfBars = 50
'Application.StatusBar = "[" & Space(NumberOfBars) & "]"


' Display and update Status Bar
    CurrentStatus = Int((Current / lastrow) * NumberOfBars)
    pctDone = Round(CurrentStatus / NumberOfBars * 100, 0)
    Application.StatusBar = Topic & " [" & String(CurrentStatus, "|") & _
                            Space(NumberOfBars - CurrentStatus) & "]" & _
                            " " & pctDone & "% Complete"

' Clear the Status Bar when you're done
'    If Current = Total Then Application.StatusBar = ""

End Sub

在此处输入图片说明

You can create a form in VBA, with code to increase the width of a label control as your code progresses.您可以在 VBA 中创建一个表单,使用代码随着代码的进展增加标签控件的宽度。 You can use the width property of a label control to resize it.您可以使用标签控件的宽度属性来调整其大小。 You can set the background colour property of the label to any colour you choose.您可以将标签的背景颜色属性设置为您选择的任何颜色。 This will let you create your own progress bar.这将让您创建自己的进度条。

The label control that resizes is a quick solution.调整大小的标签控件是一个快速解决方案。 However, most people end up creating individual forms for each of their macros.然而,大多数人最终会为他们的每个宏创建单独的表单。 I use the DoEvents function and a modeless form to use a single form for all your macros.我使用 DoEvents 函数和一个无模式表单来为您的所有宏使用一个表单。

Here is a blog post I wrote about it: http://strugglingtoexcel.wordpress.com/2014/03/27/progress-bar-excel-vba/这是我写的一篇博客文章: http : //strugglingtoexcel.wordpress.com/2014/03/27/progress-bar-excel-vba/

All you have to do is import the form and a module into your projects, and call the progress bar with: Call modProgress.ShowProgress(ActionIndex, TotalActions, Title.....)您所要做的就是将表单和模块导入到您的项目中,并使用以下命令调用进度条: Call modProgress.ShowProgress(ActionIndex, TotalActions, Title.....)

I hope this helps.我希望这有帮助。

Sub ShowProgress()
' Author    : Marecki
  Const x As Long = 150000
  Dim i&, PB$

  For i = 1 To x
    PB = Format(i / x, "00 %")
    Application.StatusBar = "Progress: " & PB & "  >>" & String(Val(PB), Chr(183)) & String(100 - Val(PB), Chr(32)) & "<<"
    Application.StatusBar = "Progress: " & PB & "  " & ChrW$(10111 - Val(PB) / 11)
    Application.StatusBar = "Progress: " & PB & "  " & String(100 - Val(PB), ChrW$(9608))
  Next i

  Application.StatusBar = ""
End SubShowProgress

Hi modified version of another post by Marecki .嗨, Marecki的另一篇文章的修改版。 Has 4 styles有4种款式

1. dots ....
2  10 to 1 count down
3. progress bar (default)
4. just percentage.

Before you ask why I didn't edit that post is I did and it got rejected was told to post a new answer.在你问我为什么不编辑那个帖子之前,我做了并且它被拒绝了被告知要发布一个新的答案。

Sub ShowProgress()

  Const x As Long = 150000
  Dim i&, PB$

  For i = 1 To x
  DoEvents
  UpdateProgress i, x
  Next i

  Application.StatusBar = ""
End Sub 'ShowProgress

Sub UpdateProgress(icurr As Long, imax As Long, Optional istyle As Integer = 3)
    Dim PB$
    PB = Format(icurr / imax, "00 %")
    If istyle = 1 Then ' text dots >>....    <<'
        Application.StatusBar = "Progress: " & PB & "  >>" & String(Val(PB), Chr(183)) & String(100 - Val(PB), Chr(32)) & "<<"
    ElseIf istyle = 2 Then ' 10 to 1 count down  (eight balls style)
        Application.StatusBar = "Progress: " & PB & "  " & ChrW$(10111 - Val(PB) / 11)
    ElseIf istyle = 3 Then ' solid progres bar (default)
        Application.StatusBar = "Progress: " & PB & "  " & String(100 - Val(PB), ChrW$(9608))
    Else ' just 00 %
        Application.StatusBar = "Progress: " & PB
    End If
End Sub

About the progressbar control in a userform, it won't show any progress if you don't use the repaint event.关于用户窗体中的progressbar控件,如果您不使用repaint事件,它将不会显示任何进度。 You have to code this event inside the looping (and obviously increment the progressbar value).您必须在循环内对此事件进行编码(并且显然会增加progressbar值)。

Example of use:使用示例:

userFormName.repaint

Just adding my part to the above collection.只需将我的部分添加到上述集合中。

If you are after less code and maybe cool UI.如果您追求更少的代码和可能很酷的 UI。 Check out my GitHub for Progressbar for VBA查看我的GitHub for Progressbar for VBA 在此处输入图片说明

a customisable one:一个可定制的:

在此处输入图片说明

The Dll is thought for MS-Access but should work in all VBA platform with minor changes. Dll 是为 MS-Access 考虑的,但应该可以在所有 VBA 平台上工作,只需稍作改动。 There is also an Excel file with samples.还有一个带有示例的 Excel 文件。 You are free to expand the vba wrappers to suit your needs.您可以自由扩展 vba 包装器以满足您的需要。

This project is currently under development and not all errors are covered.该项目目前正在开发中,并未涵盖所有错误。 So expect some!所以期待一些!

You should be worried about 3rd party dlls and if you are, please feel free to use any trusted online antivirus before implementing the dll.您应该担心 3rd 方 dll,如果您担心,请在实施 dll 之前随意使用任何可信赖的在线防病毒软件。

There have been many other great posts, however I'd like to say that theoretically you should be able to create a REAL progress bar control:还有许多其他很棒的帖子,但是我想说的是,理论上您应该能够创建一个真正的进度条控件:

  1. Use CreateWindowEx() to create the progress bar使用CreateWindowEx()创建进度条

A C++ example:一个 C++ 示例:

hwndPB = CreateWindowEx(0, PROGRESS_CLASS, (LPTSTR) NULL, WS_CHILD | WS_VISIBLE, rcClient.left,rcClient.bottom - cyVScroll,rcClient.right, cyVScroll,hwndParent, (HMENU) 0, g_hinst, NULL);

hwndParent Should be set to the parent window. hwndParent应该设置为父窗口。 For that one could use the status bar, or a custom form!为此,可以使用状态栏或自定义表单! Here's the window structure of Excel found from Spy++:这是从 Spy++ 中找到的 Excel 的窗口结构:

在此处输入图片说明

This should therefore be relatively simple using FindWindowEx() function.因此,使用FindWindowEx()函数应该相对简单。

hwndParent = FindWindowEx(Application.hwnd,,"MsoCommandBar","Status Bar")

After the progress bar has been created you must use SendMessage() to interact with the progress bar:创建进度条后,您必须使用SendMessage()与进度条交互:

Function MAKELPARAM(ByVal loWord As Integer, ByVal hiWord As Integer)
    Dim lparam As Long
    MAKELPARAM = loWord Or (&H10000 * hiWord)
End Function

SendMessage(hwndPB, PBM_SETRANGE, 0, MAKELPARAM(0, 100))
SendMessage(hwndPB, PBM_SETSTEP, 1, 0)
For i = 1 to 100
    SendMessage(hwndPB, PBM_STEPIT, 0, 0) 
Next
DestroyWindow(hwndPB)

I'm not sure how practical this solution is, but it might look somewhat more 'official' than other methods stated here.我不确定这个解决方案有多实用,但它可能看起来比这里提到的其他方法更“官方”。

Nice dialog progressbar form i looked for. 我寻找的不错的对话框进度栏形式。 progressbar from alainbryden 来自alainbryden的进度条

very simple to use, and looks nice. 使用非常简单,而且看起来不错。

edit: link works only for premium members now :/ 编辑:链接现在仅适用于高级会员:/

here is nice alternative class. 是一个不错的替代类。

Solution posted by @eykanal may not be the best in case you have huge amount of data to deal with as the enabling the status bar would slow down the code execution. 如果您有大量数据需要处理,@ eykanal发布的解决方案可能不是最好的,因为启用状态栏会减慢代码执行速度。

Following link explains a nice way to build a progress bar. 以下链接说明了构建进度条的一种好方法。 Works well with high data volume (~250K records +): 适用于高数据量(约250K条记录+):

http://www.excel-easy.com/vba/examples/progress-indicator.html http://www.excel-easy.com/vba/examples/progress-indicator.html

You can add a Form and name it as Form1, add a Frame to it as Frame1 as well as Label1 too.您可以添加一个 Form 并将其命名为 Form1,也可以将一个 Frame 添加为 Frame1 以及 Label1。 Set Frame1 width to 200, Back Color to Blue.将 Frame1 宽度设置为 200,将背景颜色设置为蓝色。 Place the code in the module and check if it helps.将代码放在模块中并检查它是否有帮助。

    Sub Main()
    Dim i As Integer
    Dim response
    Form1.Show vbModeless
    Form1.Frame1.Width = 0
    For i = 10 To 10000
        With Form1
            .Label1.Caption = Round(i / 100, 0) & "%"
            .Frame1.Width = Round(i / 100, 0) * 2
             DoEvents
        End With
    Next i

    Application.Wait Now + 0.0000075

    Unload Form1

    response = MsgBox("100% Done", vbOKOnly)

    End Sub

If you want to display on the Status Bar then you can use other way that's simpler:如果要在状态栏上显示,则可以使用其他更简单的方式:

   Sub Main()
   Dim i As Integer
   Dim response
   For i = 10 To 10000
        Application.StatusBar = Round(i / 100, 0) & "%"
   Next i

   Application.Wait Now + 0.0000075

   response = MsgBox("100% Done", vbOKOnly)

   End Sub

I know this is an old thread but I had asked a similar question not knowing about this one.我知道这是一个旧线程,但我问过一个类似的问题,但不知道这个问题。 I needed an Excel VBA Progress Bar and found this link: Excel VBA StatusBar .我需要一个 Excel VBA 进度条并找到此链接: Excel Z6E3EC7E6A9F6007B4838FC0EE7 Here is a generalized version that I wrote.这是我写的一个通用版本。 There are 2 methods, a simple version DisplaySimpleProgressBarStep that defaults to '[||有 2 种方法,一个简单版本 DisplaySimpleProgressBarStep,默认为 '[|| ] 20% Complete' and a more generalized version DisplayProgressBarStep that takes a laundry list of optional arguments so that you can make it look like just about anything you wish. ] 20% Complete' 和一个更通用的版本 DisplayProgressBarStep,它包含一个可选 arguments 的洗衣清单,这样您就可以让它看起来像您想要的任何东西。

    Option Explicit
    
    ' Resources
    '   ASCII Chart: https://vbaf1.com/ascii-table-chart/
    
    Private Enum LabelPlacement
        None = 0
        Prepend
        Append
    End Enum
    
    #If VBA7 Then
     Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
    #Else
     Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
    #End If
    
    Public Sub Test()
        Call ProgressStatusBar(Last:=10)
    End Sub
    
    Public Sub Test2()
    Const lMilliseconds As Long = 500
    Dim lIndex As Long, lNumberOfBars As Long
    Dim sBarChar As String
        sBarChar = Chr$(133) ' Elipses …
        sBarChar = Chr$(183) ' Middle dot ·
        sBarChar = Chr$(176) ' Degree sign °
        sBarChar = Chr$(171) ' Left double angle «
        sBarChar = Chr$(187) ' Right double angle »
        sBarChar = Chr$(166) ' Broken vertical bar ¦
        sBarChar = Chr$(164) ' Currency sign ¤
        sBarChar = Chr$(139) ' Single left-pointing angle quotation mark ‹
        sBarChar = Chr$(155) ' Single right-pointing angle quotation mark ›
        sBarChar = Chr$(149) ' Bullet •
        sBarChar = "|"
        
        For lIndex = 1 To 10
            Call DisplayProgressBarStep(lIndex, 10, 50, LabelPlacement.Append, sBarChar)
            Call Sleep(lMilliseconds)
        Next
        Call MsgBox("Status bar test completed.", vbOKOnly Or vbInformation, "Test2 Completed")
        Call DisplayProgressBarStep(lIndex, 10, bClearStatusBar:=True)
    End Sub
    
    Public Sub Test2Simple()
    Const lMilliseconds As Long = 500
    Dim lIndex As Long, lNumberOfBars As Long
        For lIndex = 1 To 10
            Call DisplayProgressBarStep(lIndex, 10, 50)
            Call Sleep(lMilliseconds)
        Next
        Call MsgBox("Status bar test completed.", vbOKOnly Or vbInformation, "Test2Simple Completed")
        Call DisplayProgressBarStep(lIndex, 10, bClearStatusBar:=True)
    End Sub
    
    ''' <summary>
    ''' Method to display an Excel ProgressBar. Called once for each step in the calling code process.
    ''' Defaults to vertical bar surrounded by square brackets with a trailing percentage label (e.g. [|||||] 20% Complete).
    '''
    ''' Adapted
    ''' From: Excel VBA StatusBar
    ''' Link: https://www.wallstreetmojo.com/vba-status-bar/
    ''' </summary>
    ''' <param name="Step">The current step count.</param>
    ''' <param name="StepCount">The total number of steps.</param>
    ''' <param name="NumberOfBars">Optional, Number of bars displayed for StepCount. Defaults to StepCount. The higher the number, the longer the string.</param>
    ''' <param name="LabelPlacement">Optional, Can be None, Prepend or Append. Defaults to Append.</param>
    ''' <param name="BarChar">Optional, Character that makes up the horizontal bar. Defaults to | (Pipe).</param>
    ''' <param name="PrependedBoundaryText">Optional, Boundary text prepended to the StatusBar. Defaults to [ (Left square bracket).</param>
    ''' <param name="AppendedBoundaryText">Optional, Boundary text appended to the StatusBar. Defaults to ] (Right square bracket).</param>
    ''' <param name="ClearStatusBar">Optional, True to clear the StatusBar. Defaults to False.</param>
    Private Sub DisplayProgressBarStep( _
        lStep As Long, _
        lStepCount As Long, _
        Optional lNumberOfBars As Long = 0, _
        Optional eLabelPlacement As LabelPlacement = LabelPlacement.Append, _
        Optional sBarChar As String = "|", _
        Optional sPrependedBoundaryText As String = "[", _
        Optional sAppendedBoundaryText As String = "]", _
        Optional bClearStatusBar As Boolean = False _
        )
    Dim lCurrentStatus As Long, lPctComplete As Long
    Dim sBarText As String, sLabel As String, sStatusBarText As String
        If bClearStatusBar Then
            Application.StatusBar = False
            Exit Sub
        End If
        
        If lNumberOfBars = 0 Then
            lNumberOfBars = lStepCount
        End If
        lCurrentStatus = CLng((lStep / lStepCount) * lNumberOfBars)
        lPctComplete = Round(lCurrentStatus / lNumberOfBars * 100, 0)
        sLabel = lPctComplete & "% Complete"
        sBarText = sPrependedBoundaryText & String(lCurrentStatus, sBarChar) & Space$(lNumberOfBars - lCurrentStatus) & sAppendedBoundaryText
        Select Case eLabelPlacement
            Case LabelPlacement.None: sStatusBarText = sBarText
            Case LabelPlacement.Prepend: sStatusBarText = sLabel & " " & sBarText
            Case LabelPlacement.Append: sStatusBarText = sBarText & " " & sLabel
        End Select
        Application.StatusBar = sStatusBarText
        ''Debug.Print "CurStatus:"; lCurrentStatus, "PctComplete:"; lPctComplete, "'"; sStatusBarText; "'"
    End Sub
    
    ''' <summary>
    ''' Method to display a simple Excel ProgressBar made up of vertical bars | with a trailing label. Called once for each step in the calling code process.
    '''
    ''' Adapted
    ''' From: Excel VBA StatusBar
    ''' Link: https://www.wallstreetmojo.com/vba-status-bar/
    ''' </summary>
    ''' <param name="Step">The current step count.</param>
    ''' <param name="StepCount">The total number of steps.</param>
    ''' <param name="NumberOfBars">Optional, Number of bars displayed for StepCount. Defaults to StepCount. The higher the number, the longer the string.</param>
    ''' <param name="ClearStatusBar">Optional, True to clear the StatusBar. Defaults to False.</param>
    Private Sub DisplaySimpleProgressBarStep( _
        lStep As Long, _
        lStepCount As Long, _
        Optional lNumberOfBars As Long = 0, _
        Optional bClearStatusBar As Boolean = False _
        )
        Call DisplayProgressBarStep(lStep, lStepCount, lNumberOfBars, bClearStatusBar:=bClearStatusBar)
    End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM