StatusBar Excel VBA
StatusBar adalah hak milik vba yang digunakan untuk memaparkan status kod yang selesai atau diselesaikan pada masa pelaksanaan, ia dipaparkan di sudut kiri lembaran kerja ketika makro dijalankan dan statusnya ditunjukkan dalam peratusan kepada pengguna.
Apabila makro berjalan di belakang, adalah perkara yang mengecewakan untuk menunggu tanpa mengetahui berapa lama masa yang diperlukan. Sekiranya anda berada di tahap di mana kod tersebut berjalan, anda sekurang-kurangnya dapat mengira waktu yang akan diambil. Oleh itu, idenya adalah mempunyai bar status yang menunjukkan peratusan kerja yang telah disiapkan setakat ini, seperti yang berikut.

Apa itu Application.StatusBar?
Application.StatusBar adalah harta yang boleh kita gunakan dalam pengekodan makro untuk menunjukkan status ketika makro berjalan di belakang tabir.
Ini tidak secantik "Bar Kemajuan VBA" kami tetapi cukup bagus untuk mengetahui status projek makro.

Contoh Membuat StatusBar menggunakan VBA
Ikuti langkah di bawah untuk membuat bar status.
Langkah 1: Pertama, tentukan pemboleh ubah VBA untuk mencari baris terakhir yang digunakan dalam lembaran kerja.
Kod:
Sub Status_Bar_Progress () Dim LR Sebagai Sub End Akhir

Langkah 2: Cari baris terakhir yang digunakan dengan menggunakan kod di bawah.
Kod:
Sub Status_Bar_Progress () Dim LR Selagi LR = Sel (Rows.Count, 1). End (xlUp). Sub End Sub

Langkah 3: Seterusnya, kita perlu menentukan pemboleh ubah untuk menahan bilangan bar yang akan dipaparkan.
Kod:
Sub Status_Bar_Progress () Dim LR Selagi LR = Sel (Rows.Count, 1). End (xlUp). Baris Dim NumOfBars Sebagai Sub End Integer

Ini akan menunjukkan berapa banyak bar yang dibenarkan untuk ditunjukkan di bar status.
Langkah 4: Untuk pemboleh ubah ini, simpan had bar sebagai 45.
Kod:
Sub Status_Bar_Progress () Dim LR Selagi LR = Sel (Rows.Count, 1). End (xlUp). Baris Dim NumOfBars Sebagai Integer NumOfBars = 45 End Sub

Langkah 5: Tentukan dua lagi pemboleh ubah untuk mengekalkan status semasa dan peratusan selesai semasa makro sedang berjalan.
Kod:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1). End (xlUp). Baris Dim NumOfBars Sebagai Integer NumOfBars = 45 Dim PresentStatus Sebagai Integer Dim Percetage Dilengkapkan Sebagai Integer End Sub

Langkah 6: Sekarang, untuk mengaktifkan bar status, gunakan kod di bawah.
Kod:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1) .End (xlUp) .Row Dim NumOfBars Sebagai Integer NumOfBars = 45 Dim PresentStatus As Integer Dim Percetage Dilengkapi Sebagai Aplikasi Integer.StatusBar = "(" & Space ( NumOfBars) & ")" Sub Akhir

Apa yang akan dilakukan ini akan menambahkan tanda kurung (() dan menambahkan 45 aksara spasi sebelum mengakhiri teks dengan kurungan penutup ()).
Jalankan kodnya, dan kita dapat melihat di bawah ini di bar status VBA excel.
Pengeluaran:

Langkah 7: Sekarang, kita perlu memasukkan gelung For Next di VBA untuk mengira peratusan makro yang telah selesai. Tentukan pemboleh ubah untuk memulakan makro.
Kod:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1) .End (xlUp) .Row Dim NumOfBars Sebagai Integer NumOfBars = 45 Dim PresentStatus As Integer Dim Percetage Dilengkapi Sebagai Aplikasi Integer.StatusBar = "(" & Space ( NumOfBars) & ")" Dim k Selagi k = 1 Hingga LR Seterusnya k Akhir Sub

Langkah 8: Di dalam lingkaran, kita perlu mengira apa itu "Status Sekarang". Jadi untuk pemboleh ubah "PresentStatus," kita perlu menerapkan formula seperti di bawah.
Kod:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1) .End (xlUp) .Row Dim NumOfBars Sebagai Integer NumOfBars = 45 Dim PresentStatus As Integer Dim Percetage Dilengkapi Sebagai Aplikasi Integer.StatusBar = "(" & Space ( NumOfBars) & ")" Dim k Selagi k = 1 Hingga LR HadirStatus = Int ((k / LR) * NumOfBars) Seterusnya k Akhir Sub

Kami telah menggunakan fungsi " INT " untuk mendapatkan nilai integer sebagai hasilnya.
Langkah 9: Sekarang, kita perlu mengira apa itu " Persentase Penyelesaian ", sehingga kita dapat menerapkan formula seperti yang ditunjukkan di bawah.
Kod:
Sub Status_Bar_Progress() Dim LR As Long LR = Cells(Rows.Count, 1).End(xlUp).Row Dim NumOfBars As Integer NumOfBars = 45 Dim PresentStatus As Integer Dim PercetageCompleted As Integer Application.StatusBar = "(" & Space(NumOfBars) & ")" Dim k As Long For k = 1 To LR PresentStatus = Int((k / LR) * NumOfBars) PercetageCompleted = Round(PresentStatus / NumOfBars * 100, 0) Next k End Sub

In this case, we have used the ROUND function in excel because whatever the decimal places, we need to round to the nearest zero value, so ROUND with zero as the argument has been used here.
Step 10: We have already inserted the starting bracket and end bracket to the status bar, now we need to insert the updated result, and it can be done by using the below code.
Code:
Sub Status_Bar_Progress() Dim LR As Long LR = Cells(Rows.Count, 1).End(xlUp).Row Dim NumOfBars As Integer NumOfBars = 45 Dim PresentStatus As Integer Dim PercetageCompleted As Integer Application.StatusBar = "(" & Space(NumOfBars) & ")" Dim k As Long 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" Next k End Sub
In the above code, we have inserted the opening bracket “(“ and to show the progress of the macro, we have inserted a straight line (|) by using the STRING function. When the loop is running, it will take the “PresentStatus,” and those many straight lines will be inserted in the status bar.
Code:
Application.StatusBar = "(" & String(PresentStatus, "|")
Next, we need to add space characters between one straight line to the other, so this will be calculated by using “NumOfBars” minus “PresentStatus.”
Code:
Application.StatusBar = "(" & String(PresentStatus, "|") & Space(NumOfBars - PresentStatus)
Then we close out the bracket “).” Next, we have combined the “PercentageCompleted” variable value while the loop is running with the word in front of it as “% Completed.”
Code:
Application.StatusBar = "(" & String(PresentStatus, "|") & Space(NumOfBars - PresentStatus)& _") " & PercetageCompleted & "% Complete"
When the code is running, we allow the user to access the worksheet, so we need to add “Do Events.”
Code:
Sub Status_Bar_Progress() Dim LR As Long LR = Cells(Rows.Count, 1).End(xlUp).Row Dim NumOfBars As Integer NumOfBars = 45 Dim PresentStatus As Integer Dim PercetageCompleted As Integer Application.StatusBar = "(" & Space(NumOfBars) & ")" Dim k As Long 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 Next k End Sub
Step 11: After adding “Do Events,” we can write the codes that need to be executed here.
For example, I want to insert serial numbers to the cells, so I will write code as below.’
Code:
Sub Status_Bar_Progress() Dim LR As Long LR = Cells(Rows.Count, 1).End(xlUp).Row Dim NumOfBars As Integer NumOfBars = 45 Dim PresentStatus As Integer Dim PercetageCompleted As Integer Application.StatusBar = "(" & Space(NumOfBars) & ")" Dim k As Long 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 'You can add your code here Next k End Sub
Step 12: Before we come out of the loop, we need to add one more thing, i.e., If the loop near the last used row in the worksheet then we need to make the status bar as normal.
Code:
Sub Status_Bar_Progress() Dim LR As Long LR = Cells(Rows.Count, 1).End(xlUp).Row Dim NumOfBars As Integer NumOfBars = 45 Dim PresentStatus As Integer Dim PercetageCompleted As Integer Application.StatusBar = "(" & Space(NumOfBars) & ")" Dim k As Long 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 'You can add your code here 'You can Add your code here 'You can Add your code here 'You can add your code here 'You can add your code here 'You can add your code here If k = LR Then Application.StatusBar = False Next k End Sub
Ok, we are done with coding. As you execute the code here, you can see the status bar updating its percentage completion status.
Output:

Below is the code for you.
Code:
Sub Status_Bar_Progress() Dim LR As Long LR = Cells(Rows.Count, 1).End(xlUp).Row Dim NumOfBars As Integer NumOfBars = 45 Dim PresentStatus As Integer Dim PercetageCompleted As Integer Application.StatusBar = "(" & Space(NumOfBars) & ")" Dim k As Long 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 'You can add your code here 'You can Add your code here 'You can Add your code here 'You can add your code here 'You can add your code here 'You can add your code here If k = LR Then Application.StatusBar = False Next k End Sub
Perkara yang Perlu Diingat
- Kami hanya dapat menambahkan tugas yang perlu dilakukan dalam satu gelaran.
- Anda boleh menambahkan tugas yang perlu anda lakukan setelah menambahkan prosedur "Lakukan Acara".