【发布时间】:2022-08-18 02:37:14
【问题描述】:
我正在努力让我的状态栏与我的循环正常工作。我正在使用application.screenupdating = false,这样人们就不会看到我的过程实际上有多混乱。但由于它正在运行,它可能需要 2-5 分钟才能完成。我尝试使用教程中的代码来显示进度,但它立即跳转到 100%,而不是跟踪循环数。
Public Sub ProduceReports()
Dim a As Range
Dim StartingWS As Worksheet
Dim ClientFolder As String
Dim ClientCusip
Dim ExportFile As String
Dim PreparedDate As String
Dim Exports As String
Dim AccountNumber As String
Dim LR As Long
Dim NumOfBars As Integer
Dim PresentStatus As Integer
Dim PercetageCompleted As Integer
Dim k As Long
\'******** This is my status bar code*******************
LR = Cells(Rows.Count, 1).End(xlUp).row
NumOfBars = 45
Application.StatusBar = \"[\" & Space(NumOfBars) & \"]\"
For k = 1 To LR
PresentStatus = Int((k / LR) * NumOfBars)
PercetageCompleted = Round(PresentStatus / NumOfBars * 100, 0)
Application.StatusBar = \"[\" & String(PresentStatus, \"|\") & Space(NumOfBars - PresentStatus) & \"] \" & PercetageCompleted & \"% Complete\"
DoEvents
Cells(k, 1).Value = k
Set StartingWS = ThisWorkbook.Sheets(\"Starting Page\")
\'************* This code creates the folder and sets the export path for the individual spreadsheets**********
ClientCusip = ActiveWorkbook.Worksheets(\"Starting Page\").Range(\"I11\").Value
ClientFolder = ActiveWorkbook.Worksheets(\"Starting Page\").Range(\"I10\").Value
PreparedDate = Format(Now, \"mm.yyyy\")
MkDir \"P:\\DEN-Dept\\Public\\\" & ClientFolder & \" - \" & ClientCusip & \" - \" & PreparedDate
ExportFile = \"P:\\DEN-Dept\\Public\\\" & ClientFolder & \" - \" & ClientCusip & \" - \" & PreparedDate & \"\\\"
Exports = ExportFile
Worksheets(\"Standby\").Visible = True
Sheets(\"Standby\").Activate
Application.screenUpdating = False
\'************* This is the loop to check the cells and set the offset value as elgible or ineligible**********
For Each a In StartingWS.Range(\"G9:G29\").Cells
If a.Value = \"Eligible\" Then
AccountNumber = a.Offset(0, -1).Value
PrepareClassSheets AccountNumber, Exports
End If
Next a
Sheets(\"Starting Page\").Activate
Application.screenUpdating = True
Worksheets(\"Standby\").Visible = False
MsgBox Prompt:=\"Class Action Data for\" & \" \" & ClientFolder & \" \" & \"has been prepared.\", Title:=\"Bear has completed his tasks.\"
Call Shell(\"explorer.exe\" & \" \" & ExportFile, vbNormalFocus)
\'************** End of the status bar*********
If k = LR Then Application.StatusBar = False
Next k
End Sub
我想我想如果我在另一个循环之外关闭状态栏循环,它会起作用。我在这里遗漏了一些明显的东西吗?
-
当我将您的代码简化为仅进度条部分时,它对我来说效果很好(只有这样我才能测试它)。
LR(你的最后一个行号)在你第一次设置的时候是什么值?如果您通过代码调试步骤,进度条是否也不会显示? -
我的 LR 设置为 45,只是因为这就是示例所具有的。进度条出现了,但假设在 G9:G29 之间有 8 个符合条件的帐户,在循环开始之前它就达到了 100。所以它似乎无法判断它是一个循环中的一个循环。
-
在一个将在几分钟内完成的过程中,它会立即以 100% 的速度开始吗?
-
要“检查”StatusBar 进程的中间值,请将这行代码放在“DoEvents”之前,运行代码并打开(必要时按CTRL+G)即时窗口以查看这些中间值。也许你可以得出一些结论。另一个想法是,也许另一个函数正在同时处理状态栏。你能检查一下名为 \'PrepareClassSheets\' 的子例程是否也使用它吗?... 代码:
Debug.Print Now, k, LR, Application.StatusBar