Excel VBA StatusBar
A StatusBar egy vba tulajdonsága, amely a végrehajtáskor elkészült vagy befejezett kód állapotának megjelenítésére szolgál, a makró végrehajtásakor a munkalap bal oldali sarkában jelenik meg, és az állapot százalékban jelenik meg a felhasználónak.
Amikor a makró fut, frusztráló dolog várni anélkül, hogy tudnánk, mennyi időbe telik. Ha éppen abban a szakaszban van, ahol a kód fut, akkor legalább kiszámíthatja az időtartamot. Tehát az az elképzelés, hogy egy olyan állapotjelző sáv jelenjen meg, amely a munka százalékos arányát fejezte be eddig, hasonlóan az alábbihoz.

Mi az Application.StatusBar?
Az Application.StatusBar az a tulajdonság, amelyet a makrókódolásban használhatunk az állapot megjelenítéséhez, amikor a makró a színhely mögött fut.
Ez nem olyan szép, mint a „VBA Progress Bar”, de elég jó ahhoz, hogy ismerjük a makroprojekt állapotát.

Példa a StatusBar létrehozására a VBA használatával
Az állapotsor létrehozásához kövesse az alábbi lépéseket.
1. lépés: Először határozza meg a VBA változót, hogy megtalálja a munkalapon utoljára használt sort.
Kód:
Alállapot_Sáv_Folyamat () Dim LR As Long End Sub

2. lépés: Az alábbi kód használatával keresse meg az utoljára használt sort.
Kód:
Sub Status_Bar_Progress () Dim LR As Long LR = cellák (Rows.Count, 1) .End (xlUp) .Row End Sub

3. lépés: Ezután meg kell határoznunk a változót a megjelenítendő oszlopok számának megőrzéséhez.
Kód:
Alállapot_Sáv_Folyamat () Dim LR As Long LR = cellák (Rows.Count, 1) .End (xlUp) .Dim NumOfBars sor egészének vége Sub

Ez megtartja, hogy hány sáv jelenhet meg az állapotsorban.
4. lépés: Ehhez a változóhoz a sáv határát 45-ként tárolja.
Kód:
Alállapot_Sáv_Folyamat () Dim LR As Long LR = cellák (Rows.Count, 1) .End (xlUp). Sor Dim NumOfBars egész számként NumOfBars = 45 End Sub

5. lépés: Adjon meg még két változót az aktuális állapot és a makró futása közben befejezett százalékos arányának megtartásához.
Kód:
Sub Status_Bar_Progress () Dim LR As Long LR = cellák (Rows.Count, 1) .End (xlUp) .Row Dim NumOfBars as Integer NumOfBars = 45 Dim PresentStatus As Integer Dim PercetageCompleted as Integer End Sub

6. lépés: Most az állapotsor engedélyezéséhez használja az alábbi kódot.
Kód:
Alállapot_Sáv_Folyamat () Dim LR As Long LR = cellák (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

Ez meg fogja tenni a zárójelet (() és 45 szóközt, mielőtt a szöveg záró zárójelrel () zárul.
Futtassa a kódot, és az alábbiakban láthattuk az excel VBA állapotsorát.
Kimenet:

7. lépés: Most be kell vonnunk a For Next ciklust a VBA-ba a befejezett makró százalékának kiszámításához. Definiáljon egy változót a makró elindításához.
Kód:
Sub Status_Bar_Progress () Dim LR As Long LR = cellák (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 Meddig k = 1-ig LR-ig Következő k Vége Sub

8. lépés: A hurok belsejében ki kell számolnunk, mi a „Jelen állapot”. Tehát a „PresentStatus” változóhoz az alábbi képletet kell alkalmaznunk.
Kód:
Sub Status_Bar_Progress () Dim LR As Long LR = cellák (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 Meddig k = 1 Az LR PresentStatus = Int ((k / LR) * NumOfBars) Következő k End Sub

Az „ INT ” függvényt használtuk az egész szám eredményének megszerzésére.
9. lépés: Most ki kell számolnunk, hogy mi a „ százalékos teljesítés ”, így alkalmazhatjuk az alábbi képletet.
Kód:
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
Dolgok, amikre emlékezni kell
- Csak azokat a feladatokat adhatjuk hozzá, amelyeket a cikluson belül kell elvégezni.
- Hozzáadhatja azokat a feladatokat, amelyeket meg kell tennie az „Események végrehajtása” eljárás hozzáadása után.