Stavový řádek Excel VBA
StatusBar je vlastnost vba, která se používá k zobrazení stavu kódu dokončeného nebo dokončeného v době provádění, zobrazuje se v levém rohu listu, když se provádí makro, a stav se zobrazuje v procentech uživateli.
Když makro běží pozadu, je frustrující věc čekat, aniž byste věděli, jak dlouho to bude trvat. Pokud jste ve fázi, kdy je kód spuštěn, můžete alespoň vypočítat čas, který bude trvat. Myšlenkou tedy je mít stavový řádek, který ukazuje procento práce, která byla doposud dokončena, jako níže.

Co je Application.StatusBar?
Application.StatusBar je vlastnost, kterou můžeme použít v kódování maker k zobrazení stavu, kdy je makro spuštěno za scénou.
To není tak krásné jako naše „VBA Progress Bar“, ale dost dobré na to, abychom poznali stav makroprojektu.

Příklad vytvoření StatusBar pomocí VBA
Podle následujících pokynů vytvořte stavový řádek.
Krok 1: Nejprve definujte proměnnou VBA, abyste našli poslední použitý řádek v listu.
Kód:
Sub Status_Bar_Progress () Dim LR jako Long End Sub

Krok 2: Najděte poslední použitý řádek pomocí níže uvedeného kódu.
Kód:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1) .End (xlUp) .Row End Sub

Krok 3: Dále musíme definovat proměnnou, která bude obsahovat počet pruhů, které se mají zobrazit.
Kód:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1) .End (xlUp). Row Dim NumOfBars As Integer End Sub

Tím se udrží, kolik pruhů je povoleno zobrazovat ve stavovém řádku.
Krok 4: U této proměnné uložte limit pruhu jako 45.
Kód:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1). End (xlUp). Row Dim NumOfBars As Integer NumOfBars = 45 End Sub

Krok 5: Definujte další dvě proměnné, které udrží aktuální stav a procento dokončené, když je makro spuštěno.
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 End Sub

Krok 6: Chcete-li stavový řádek povolit, použijte níže uvedený kód.
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) & ")" End Sub

Co to udělá, přidá závorku (() a přidá 45 mezer před koncem textu s uzavírací závorkou ()).
Spusťte kód a na stavovém řádku aplikace Excel VBA jsme mohli vidět níže.
Výstup:

Krok 7: Nyní musíme do VBA zahrnout smyčku For Next pro výpočet procenta makra, které bylo dokončeno. Definujte proměnnou pro spuštění makra.
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 Next k End Sub

Krok 8: Uvnitř smyčky musíme vypočítat, co je „Současný stav“. U proměnné „PresentStatus“ tedy musíme použít vzorec níže.
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) Další k End Sub

K získání celočíselné hodnoty jsme použili funkci „ INT “.
Krok 9: Nyní musíme vypočítat, co je „ Procento dokončení “, abychom mohli použít vzorec, jak je znázorněno níže.
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) Další k Konec Sub

V tomto případě jsme použili funkci ROUND v aplikaci Excel, protože bez ohledu na desetinná místa musíme zaokrouhlit na nejbližší nulovou hodnotu, takže zde byl použit ROUND s nulou jako argumentem.
Krok 10: Počáteční závorku a koncovou závorku jsme již vložili do stavového řádku, nyní musíme vložit aktualizovaný výsledek a lze to provést pomocí níže uvedeného kódu.
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) Application.StatusBar = "(" & String ( PresentStatus, "|") & Space (NumOfBars - PresentStatus) & _ ")" & PercetageCompleted & "% Complete" Další k Konec Sub
Ve výše uvedeném kódu jsme vložili úvodní závorku „(„ a pro zobrazení postupu makra jsme vložili přímku (|) pomocí funkce STRING. Když smyčka běží, bude trvat „ PresentStatus , “A těch mnoho přímek bude vloženo do stavového řádku.
Kód:
Application.StatusBar = "(" & String (PresentStatus, "|"))
Dále musíme přidat mezery mezi jednu přímku do druhé, takže to bude vypočítáno pomocí „NumOfBars“ minus „PresentStatus“.
Kód:
Application.StatusBar = "(" & String (PresentStatus, "|") & Space (NumOfBars - PresentStatus)
Potom uzavřeme závorku „).“ Dále jsme kombinovali hodnotu proměnné „PercentageCompleted“, zatímco smyčka běží se slovem před ní jako „% Completed“.
Kód:
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
Věci k zapamatování
- Můžeme přidat pouze úkoly, které je třeba provést ve smyčce.
- Úkoly, které musíte udělat, můžete přidat po přidání postupu „Do Events“.