StatusBar VBA - Come abilitare StatusBar utilizzando il codice VBA?

Barra di stato VBA di Excel

StatusBar è la proprietà di una vba che viene utilizzata per visualizzare lo stato del codice finito o completato al momento dell'esecuzione, viene visualizzata nell'angolo sinistro del foglio di lavoro quando viene eseguita una macro e lo stato viene mostrato in percentuale all'utente.

Quando la macro è in ritardo, è frustrante aspettare senza sapere quanto tempo ci vorrà. Se ti trovi nella fase in cui il codice è in esecuzione, puoi almeno calcolare il tempo necessario. Quindi, l'idea è di avere una barra di stato che mostri la percentuale di lavoro che è stata completata finora, come quella sotto.

Cos'è Application.StatusBar?

Application.StatusBar è la proprietà che possiamo usare nella codifica delle macro per mostrare lo stato quando la macro è in esecuzione dietro le quinte.

Questo non è bello come la nostra "barra di avanzamento VBA" ma abbastanza buono per conoscere lo stato del progetto macro.

Esempio per creare StatusBar utilizzando VBA

Segui i passaggi seguenti per creare una barra di stato.

Passaggio 1: in primo luogo, definire la variabile VBA per trovare l'ultima riga utilizzata nel foglio di lavoro.

Codice:

Sub Status_Bar_Progress () Dim LR As Long End Sub

Passaggio 2: trova l'ultima riga utilizzata utilizzando il codice seguente.

Codice:

Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1) .End (xlUp) .Row End Sub

Passaggio 3: Successivamente, dobbiamo definire la variabile per contenere il numero di barre da visualizzare.

Codice:

Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1) .End (xlUp) .Row Dim NumOfBars As Integer End Sub

Questo manterrà quante barre possono essere visualizzate nella barra di stato.

Passaggio 4: per questa variabile, memorizzare il limite della barra come 45.

Codice:

Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1) .End (xlUp) .Row Dim NumOfBars As Integer NumOfBars = 45 End Sub

Passaggio 5: definire altre due variabili per mantenere lo stato corrente e la percentuale di completamento quando la macro è in esecuzione.

Codice:

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 End Sub

Passaggio 6: ora, per abilitare la barra di stato, utilizzare il codice seguente.

Codice:

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) & ")" End Sub

Ciò che farà aggiungerà la parentesi (() e aggiungerà 45 spazi prima che il testo termini con la parentesi chiusa ()).

Esegui il codice e potremmo vedere quanto segue nella barra di stato VBA di Excel.

Produzione:

Passaggio 7: ora, è necessario includere il ciclo For Next in VBA per calcolare la percentuale della macro che è stata completata. Definisci una variabile per avviare la macro.

Codice:

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 Next k End Sub

Passaggio 8: all'interno del ciclo, dobbiamo calcolare qual è lo "stato attuale". Quindi per la variabile "PresentStatus", dobbiamo applicare la formula come di seguito.

Codice:

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) Next k End Sub

Abbiamo utilizzato la funzione " INT " per ottenere il valore intero come risultato.

Passaggio 9: Ora, dobbiamo calcolare qual è la " Percentuale di completamento ", in modo da poter applicare la formula come mostrato di seguito.

Codice:

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

Cose da ricordare

  • Possiamo aggiungere solo le attività che devono essere eseguite all'interno del ciclo.
  • È possibile aggiungere le attività che è necessario eseguire dopo aver aggiunto la procedura "Esegui eventi".

Articoli interessanti...