広告
エクセルのマクロ(VBA)で処理の進捗状況を表示する。
目次
プログレスバーのフォーム作成
フォームの追加

コントロールボックスにプログレスバーを追加



ラベル、ボタン、バーを追加してそれっぽく作成

ソースコード作成
UserFormに中断ボタン実行を判定するフラグを追加する
UserForm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
'中断ボタン実行フラグ Public IsCancel As Boolean Private Sub CommandButton1_Click() '中断ボタン実行でフラグをオン IsCancel = True End Sub Private Sub Label1_Click() End Sub Private Sub ProgressBar1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS) End Sub Private Sub UserForm_Click() End Sub |
Moduleにプログレスバー表示処理を追加
Main Module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
'Sleepを使うための定義 #If VBA7 Then Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As LongPtr) #Else Private Declare Sub Sleep Lib "kernel32" (ByVal ms As Long) #End If Sub ProgressBar() Const MaxCount As Long = 100 UserForm2.Show P_bar UserForm2.ProgressBar1.Min = 1 UserForm2.ProgressBar1.Max = MaxCount UserForm2.IsCancel = False Dim index As Long Dim progress As Long For index = 1 To MaxCount Sleep (100) progress = CInt((index / MaxCount) * 100) UserForm2.Label1.Caption = index & "%完了" UserForm2.ProgressBar1.Value = index If UserForm2.IsCancel = True Then End End If DoEvents Next Unload UserForm2 End Sub |
実行結果

広告
広告