これは、コードの大幅にコメントされたバージョンです。
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