Thanh trạng thái VBA trong Excel
StatusBar là thuộc tính của vba được sử dụng để hiển thị trạng thái của mã đã hoàn thành hoặc đã hoàn thành tại thời điểm thực thi, nó được hiển thị ở góc bên trái của trang tính khi macro được thực thi và trạng thái được hiển thị theo tỷ lệ phần trăm cho người dùng.
Khi macro đang chạy phía sau, thật khó chịu khi phải chờ đợi mà không biết sẽ mất bao lâu. Nếu bạn đang ở giai đoạn mã đang chạy, ít nhất bạn có thể tính toán thời gian mà mã sẽ mất. Vì vậy, ý tưởng là có một thanh trạng thái hiển thị phần trăm công việc đã được hoàn thành cho đến nay, giống như hình dưới đây.

Application.StatusBar là gì?
Application.StatusBar là thuộc tính mà chúng ta có thể sử dụng trong mã hóa macro để hiển thị trạng thái khi macro đang chạy phía sau cảnh.
Nó không đẹp như “VBA Progress Bar” của chúng tôi nhưng đủ tốt để biết tình trạng của dự án vĩ mô.

Ví dụ để tạo StatusBar bằng VBA
Làm theo các bước dưới đây để tạo thanh trạng thái.
Bước 1: Đầu tiên, xác định biến VBA để tìm hàng được sử dụng cuối cùng trong trang tính.
Mã:
Sub Status_Bar_Progress () Dim LR As Long End Sub

Bước 2: Tìm hàng được sử dụng cuối cùng bằng cách sử dụng mã bên dưới.
Mã:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1) .End (xlUp) .Row End Sub

Bước 3: Tiếp theo, chúng ta cần xác định biến giữ số lượng thanh sẽ hiển thị.
Mã:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1) .End (xlUp) .Row Dim NumOfBars As Integer End Sub

Điều này sẽ chứa số lượng thanh được phép hiển thị trên thanh trạng thái.
Bước 4: Đối với biến này, lưu trữ giới hạn của thanh là 45.
Mã:
Sub Status_Bar_Progress () Dim LR As Long LR = Cells (Rows.Count, 1) .End (xlUp) .Row Dim NumOfBars As Integer NumOfBars = 45 End Sub

Bước 5: Xác định thêm hai biến để giữ trạng thái hiện tại và tỷ lệ phần trăm đã hoàn thành khi macro đang chạy.
Mã:
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

Bước 6: Bây giờ, để bật thanh trạng thái, hãy sử dụng đoạn mã dưới đây.
Mã:
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

Điều này sẽ thực hiện nó sẽ thêm dấu ngoặc (() và thêm 45 ký tự khoảng trắng trước khi kết thúc văn bản bằng dấu ngoặc đóng ()).
Thực thi mã và chúng ta có thể thấy thông tin bên dưới trong thanh trạng thái VBA của excel.
Đầu ra:

Bước 7: Bây giờ, chúng ta cần bao gồm vòng lặp For Next trong VBA để tính toán phần trăm macro đã được hoàn thành. Xác định một biến để bắt đầu macro.
Mã:
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

Bước 8: Bên trong vòng lặp, chúng ta cần tính “Trạng thái hiện tại” là gì. Vì vậy, đối với biến "PresentStatus", chúng ta cần áp dụng công thức như dưới đây.
Mã:
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

Kết quả là chúng tôi đã sử dụng hàm “ INT ” để lấy giá trị nguyên.
Bước 9: Bây giờ, chúng ta cần tính “ Phần trăm hoàn thành ” là bao nhiêu, vì vậy chúng ta có thể áp dụng công thức như hình dưới đây.
Mã:
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
Những điều cần ghi nhớ
- Chúng tôi chỉ có thể thêm các tác vụ cần thực hiện trong vòng lặp.
- Bạn có thể thêm các công việc mà bạn cần làm sau khi thêm quy trình "Thực hiện sự kiện".