【问题标题】:How to combine my vba working with a progress bar?如何将我的 vba 与进度条结合使用?
【发布时间】:2016-08-17 18:11:34
【问题描述】:

我想参考这个进度条示例。 http://spreadsheetpage.com/index.php/tip/displaying_a_progress_indicator/

关于我的 vb 工作,我将生成一个在整个工作表中使用 ADO 的报告。由于报告生成时间太长(1分钟),我想在报告生成过程中实现一个进度条。顺便说一句,报告将在一个新的excel文件中生成。

 Private Sub CommandButton3_Click()



    Dim sSQLQry As String
    Dim ReturnArray

    Dim Conn As New ADODB.Connection
    Dim mrs As New ADODB.Recordset

    Dim DBPath As String, sconnect As String



    DBPath = ThisWorkbook.FullName


    sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
    Conn.Open sconnect

   sSQLSting = "..."

   Set rs = Conn.Execute(sSQLSting)

   j = 6

    Do While Not rs.EOF
with thisworkbook.worksheets("report")
.Cells(j, 1) = rs.Fields(0).Value
.Cells(j, 3) = rs.Fields(2).Value
.Cells(j, 4) = rs.Fields(3).Value
.Cells(j, 7) = rs.Fields(6).Value
End with 


 j = j + 1

  rs.MoveNext

Loop


rs.Close

Dim wb As Workbook
    Set wb = Workbooks.Add

    ThisWorkbook.Sheets("report").Copy Before:=wb.Sheets(1)

...copy Sheets("report") to wb ...

strFileName = "c:\Users\" & Environ("Username") & "\Desktop\" & ThisWorkbook.Sheets("report").Cells(1, 1) & ".xlsx"
'End With
wb.SaveAs strFileName

我阅读了进度条码。它需要使用循环变量PctDone = Counter / (RowMax * ColMax)。对于我的代码,它包括不同的工作——SQL 计算、粘贴到工作表(“报告”)、将工作表(“报告”)复制到新工作簿。因此,我不知道如何使我的代码与这个进度条应用程序相匹配。

参考 Progress bar in VBA Excel


如果在我的情况下无法实现进度条,我该怎么做才能让用户知道“请耐心等待大约 1 分钟”?

【问题讨论】:

  • 如果不知道 a) 您要处理的项目总数(数据库中的行数)和 b) 您已处理的行数的计数器,您将无法计算完成百分比加工完毕。第二个很简单 - 添加一个变量,每次通过循环时递增。只有在开始循环之前可以确定有多少行时,第一个才是可能的 - 你能做到吗?
  • @Ken White 我不知道每个报告的确切行,因为我使用End(xlUp).row 完成新工作簿中的最终报告。另外,我不知道 SQL 所在的行将产生,以及花费在制作和编写新工作条上的时间。
  • 您不需要使用进度条本身,因为@KenWhite 提到您无法计算百分比。话说回来。有一个替代方案。让我搜索我的项目数据库。我有一个样本,如果我找到它,我会发布它。它涉及列表框和用户窗体
  • @Siddharth Rout 我尝试使用显示“请稍等一分钟”的用户表单,但我发现它需要花费一些时间来加载新的用户表单。这使得整个应用程序的加载时间很长。
  • 如果您不知道这两件事,就无法显示显示完成百分比的进度条。您可以显示一个选取框样式的进度条(只是与小绿色部分来回移动,但不显示您走了多远);您可能可以通过 Google 在 VBA 中找到这样做的示例。

标签: vba excel


【解决方案1】:

您本身不需要使用进度条,因为您无法计算已完成工作的百分比。在这种情况下,最好让用户知道您(或代码在做什么)。您可以使用Application.StatusBar 进行更新,但我们当中有多少人真正往下看?此外,没有什么比弹出一个表单并更新您的状态更有趣的了……如果您愿意,您也可以在用户表单上使用Animated GIFS

我尝试使用显示“请稍等一分钟”的用户表单,但我发现它需要花费一些时间来加载新的用户表单。这使得整个应用程序的加载时间很长

好的,您永远不会在用户窗体的UserForm_Initialize() 事件中显示进度。显示进程实际开始时的进度。如果需要,将所有内容移至UserForm_Activate() 或单击Commandbutton。我使用UserForm_Click() 进行演示。

假设我们有一个如下所示的用户表单,带有 Frame 和 Listbox` 控件。

将此代码放入用户表单中

Private Sub UserForm_Click()
    ListBox1.AddItem "I am performing something in a loop..."
    ListBox1.Selected(ListBox1.ListCount - 1) = True

    For i = 1 To 10
        Wait 3
    Next i

    ListBox1.AddItem "I am now writing something to the workbook..."
    ListBox1.Selected(ListBox1.ListCount - 1) = True

    Range("A1").Value = "Sid"

    ListBox1.AddItem "I am performing something again in a loop..."
    ListBox1.Selected(ListBox1.ListCount - 1) = True

    For i = 1 To 10
        Wait 3
    Next i

    '
    '~~> And So on
    '
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

逻辑

  1. 在开始任何进程之前,将描述添加到列表框中。我在上面的代码中添加了示例流程和描述。请根据您的需要修改它们。
  2. 注意到ListBox1.Selected(ListBox1.ListCount - 1) = True这行了吗?这将确保始终选择最近的条目。如果列表框中添加了许多内容,这还可以确保列表框滚动到最新条目。

行动中

编辑

你误解了它的工作原理:)

  1. 如上图所示在表单上添加一个列表框。
  2. 从用户窗体中删除所有代码并用此代码替换它

现在运行代码。

Private Sub UserForm_Activate()
    ListBox1.AddItem "Generating random numbers..."
    ListBox1.Selected(ListBox1.ListCount - 1) = True
    DoEvents

    For i = 1 To 1000
        For j = 1 To 1000
            ThisWorkbook.Sheets("content").Cells(i, j) = Rnd
        Next
    Next

    ListBox1.AddItem "Copying and working with Content sheet..."
    ListBox1.Selected(ListBox1.ListCount - 1) = True
    DoEvents

    Row = ThisWorkbook.Sheets("content").Range("A" & Rows.Count).End(xlUp).Row

    Set wb = Workbooks.Add

    ThisWorkbook.Sheets("content").Copy Before:=wb.Sheets(1)

    wb.Sheets(1).Cells(Row, 1) = Application.WorksheetFunction.Sum(wb.Sheets(1).Range("A:A"))
    wb.Sheets(1).Cells(Row, 2) = Application.WorksheetFunction.Sum(wb.Sheets(1).Range("B:B"))
    wb.Sheets(1).Cells(Row, 3) = Application.WorksheetFunction.Sum(wb.Sheets(1).Range("C:C"))
    wb.Sheets(1).Cells(Row, 4) = Application.WorksheetFunction.Sum(wb.Sheets(1).Range("D:D"))
    wb.Sheets(1).Cells(Row, 5) = Application.WorksheetFunction.Sum(wb.Sheets(1).Range("E:E"))
    wb.Sheets(1).Cells(Row, 6) = Application.WorksheetFunction.Sum(wb.Sheets(1).Range("F:F"))
    wb.Sheets(1).Cells(Row, 7) = Application.WorksheetFunction.Sum(wb.Sheets(1).Range("G:G"))
    wb.Sheets(1).Cells(Row, 8) = Application.WorksheetFunction.Sum(wb.Sheets(1).Range("H:H"))
    wb.Sheets(1).Cells(Row, 9) = Application.WorksheetFunction.Sum(wb.Sheets(1).Range("I:I"))
    wb.Sheets(1).Cells(Row, 10) = Application.WorksheetFunction.Sum(wb.Sheets(1).Range("K:K"))

    ListBox1.AddItem "Saving File..."
    ListBox1.Selected(ListBox1.ListCount - 1) = True
    DoEvents

    strFileName = "c:\Users\" & Environ("Username") & "\Desktop\" & ThisWorkbook.Sheets("content").Cells(1, 1) & ".xlsx"

    wb.SaveAs strFileName
    ThisWorkbook.Sheets("content").Cells.Clear

    ListBox1.AddItem "Done!"
    ListBox1.Selected(ListBox1.ListCount - 1) = True
    DoEvents
End Sub

【讨论】:

  • 我已经添加了这个用户表单和代码。那么,如何与我的按钮结合?单击 button1 时,此用户窗体打开。与我的按钮组合的程序是什么(运行这些操作)?
  • 如果Private Sub CommandButton3_Click() 启动这个用户表单,那么在用户表单的激活事件中,输入所有代码。此外,如果需要,请在每个 ListBox1.Selected(ListBox1.ListCount - 1) = True 之后添加 DoEvents
  • ListBox1.AddItem "I am performing something in a loop..." ListBox1.Selected(ListBox1.ListCount - 1) = True 错误 424 需要对象
  • 没有。你做错了。将userform.show放入CommandButton3_Clic‌​k‌​()和commandbutton已有的代码中,移动到上面userform的activate事件中
  • 现在,我已经完成了以下步骤:1)创建用户表单 2)将您的代码放在 UserForm_Click 3)在我的点击按钮上只添加 1 句话:userform.show 4)将我所有的命令按钮的现有代码移动到用户窗体的激活事件
猜你喜欢
  • 2010-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-22
  • 1970-01-01
  • 2021-11-20
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多