Excel VBA StatusBar
StatusBar είναι η ιδιότητα ενός vba που χρησιμοποιείται για την εμφάνιση της κατάστασης του κώδικα που έχει ολοκληρωθεί ή ολοκληρωθεί τη στιγμή της εκτέλεσης, εμφανίζεται στην αριστερή γωνία του φύλλου εργασίας όταν εκτελείται μια μακροεντολή και η κατάσταση εμφανίζεται σε ποσοστό στον χρήστη.
Όταν η μακροεντολή τρέχει πίσω, είναι απογοητευτικό να περιμένετε χωρίς να γνωρίζετε πόσο καιρό θα διαρκέσει. Εάν βρίσκεστε στο στάδιο όπου εκτελείται ο κωδικός, μπορείτε τουλάχιστον να υπολογίσετε τον χρόνο που θα χρειαστεί. Έτσι, η ιδέα είναι να υπάρχει μια γραμμή κατάστασης που να δείχνει το ποσοστό εργασίας που έχει ολοκληρωθεί μέχρι στιγμής, όπως το παρακάτω.

Τι είναι το Application.StatusBar;
Application.StatusBar είναι η ιδιότητα που μπορούμε να χρησιμοποιήσουμε στην κωδικοποίηση μακροεντολών για να δείξουμε την κατάσταση όταν η μακροεντολή εκτελείται πίσω από τη σκηνή.
Αυτό δεν είναι τόσο όμορφο όσο το "VBA Progress Bar" αλλά αρκετά καλό για να γνωρίζουμε την κατάσταση του έργου μακροεντολών.

Παράδειγμα δημιουργίας StatusBar χρησιμοποιώντας VBA
Ακολουθήστε τα παρακάτω βήματα για να δημιουργήσετε μια γραμμή κατάστασης.
Βήμα 1: Αρχικά, ορίστε τη μεταβλητή VBA για να βρείτε την τελευταία γραμμή που χρησιμοποιήθηκε στο φύλλο εργασίας.
Κώδικας:
Sub Status_Bar_Progress () Dim LR ως Long End Sub

Βήμα 2: Βρείτε τη γραμμή που χρησιμοποιήθηκε τελευταία χρησιμοποιώντας τον παρακάτω κώδικα.
Κώδικας:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1). End (xlUp). Sub End Sub

Βήμα 3: Στη συνέχεια, πρέπει να καθορίσουμε τη μεταβλητή για να κρατήσουμε τον αριθμό των γραμμών που θα εμφανίζονται
Κώδικας:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1). End (xlUp). Row Dim NumOfBars As Integer End Sub

Αυτό θα κρατήσει πόσες γραμμές επιτρέπεται να εμφανίζονται στη γραμμή κατάστασης.
Βήμα 4: Για αυτήν τη μεταβλητή, αποθηκεύστε το όριο της γραμμής ως 45.
Κώδικας:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1). End (xlUp). Row Dim NumOfBars As Integer NumOfBars = 45 End Sub

Βήμα 5: Ορίστε δύο ακόμη μεταβλητές για να διατηρήσετε την τρέχουσα κατάσταση και το ποσοστό που ολοκληρώθηκε κατά την εκτέλεση της μακροεντολής.
Κώδικας:
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 Percetage Ολοκληρώθηκε ως Integer End Sub

Βήμα 6: Τώρα, για να ενεργοποιήσετε τη γραμμή κατάστασης, χρησιμοποιήστε τον παρακάτω κώδικα.
Κώδικας:
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

Αυτό που θα κάνει θα προσθέσει την αγκύλη (() και θα προσθέσει 45 χαρακτήρες διαστήματος πριν τελειώσει το κείμενο με αγκύλη κλεισίματος ()).
Εκτελέστε τον κώδικα και θα μπορούσαμε να δούμε τα παρακάτω στη γραμμή κατάστασης excel VBA.
Παραγωγή:

Βήμα 7: Τώρα, πρέπει να συμπεριλάβουμε το βρόχο For Next στο VBA για να υπολογίσουμε το ποσοστό της μακροεντολής που έχει ολοκληρωθεί. Ορίστε μια μεταβλητή για να ξεκινήσετε τη μακροεντολή.
Κώδικας:
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

Βήμα 8: Μέσα στο βρόχο, πρέπει να υπολογίσουμε τι είναι η «παρούσα κατάσταση». Έτσι, για τη μεταβλητή "PresentStatus", πρέπει να εφαρμόσουμε τον τύπο όπως παρακάτω.
Κώδικας:
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

Χρησιμοποιήσαμε τη συνάρτηση " INT " για να πάρουμε την ακέραια τιμή ως αποτέλεσμα.
Βήμα 9: Τώρα, πρέπει να υπολογίσουμε τι είναι το " Ποσοστό ολοκλήρωσης ", ώστε να μπορούμε να εφαρμόσουμε τον τύπο όπως φαίνεται παρακάτω.
Κώδικας:
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
Πράγματα που πρέπει να θυμάστε
- Μπορούμε να προσθέσουμε μόνο τις εργασίες που πρέπει να γίνουν εντός του βρόχου.
- Μπορείτε να προσθέσετε τις εργασίες που πρέπει να κάνετε μετά την προσθήκη της διαδικασίας "Εκδηλώσεις".