
我對這個問題感到非常困惑,我有一個嵌套的 for-next 循環,它提供了超過一千個循環,我透過看到這段程式碼知道這一點:
'find row ranges for department 1-10
For i = 1 To 10 Step 1
Dim tempRange As Range
Set tempRange = GetRowRange(importsheet, DepColumn, i)
'and iterate through the columns to insert them
' find row ranges for section
If Not (importsheet.UsedRange.Find("afdeling_" & i) Is Nothing) Then
Dim SecColumn
Dim secRange As Range
SecColumn = importsheet.UsedRange.Find("afdeling_" & i).column
Set bCell = tempRange.Columns(SecColumn)
tempRange.Sort Key1:=bCell, Order1:=xlAscending, Header:=xlYes
For ix = 1 To 10 Step 1
'check for a valid section column
Set secRange = GetRowRange(tempRange, SecColumn, ix)
totalposts = totalposts + IterateColumns(secRange, spgsheet, importsheet, debugsheet, year, month, week, Hospital, i, ix, varType, False)
Progress
Next ix
Else
totalposts = totalposts + IterateColumns(tempRange, spgsheet, importsheet, debugsheet, year, month, week, Hospital, i, 0, varType, False)
End If
Progress
Next i
我的進度函數如下所示:
Function Progress()
iProgress = iProgress + 1
Application.StatusBar = Format(iProgress, "0%") & " Completed"
End Function
但進度條經常會顯示到3300%。
這怎麼可能?
答案1
您沒有顯示所有代碼。例如,我假設 thstiProgress
是全域定義的,但我們在這裡看不到這一點。
另外,為什麼要DIM
在迴圈內執行語句?您應該只執行一次它們,並且nothing
一旦完成它們就應該將物件變數設為。如果不這樣做,可能會導致一些令人討厭的記憶體問題。
回答具體問題。您已設定iProgress
為計數器而不是%。
要獲得百分比,您需要知道在啟動進度條之前將迭代多少項。然後您需要當前項目計數器和總數。CurrentCount/TotalItemCount
給你進步。