VBA StatusBar - कैसे VBA कोड का उपयोग करके StatusBar को सक्षम करें?

एक्सेल VBA स्टेटसबार

StatusBar एक vba की संपत्ति है जिसका उपयोग निष्पादन के समय समाप्त या पूर्ण किए गए कोड की स्थिति प्रदर्शित करने के लिए किया जाता है, यह कार्यपत्रक के बाएँ हाथ के कोने पर प्रदर्शित होता है जब मैक्रो निष्पादित होता है और स्थिति प्रतिशत में दिखाई जाती है उपयोगकर्ता के लिए।

जब मैक्रो पीछे चल रहा होता है, तो यह जानने के लिए इंतजार करने के लिए एक निराशा की बात है कि यह कितना समय लगेगा। यदि आप उस चरण में हैं जहां कोड चल रहा है, तो आप कम से कम उस समय की गणना कर सकते हैं जो लेने वाला है। इसलिए, विचार यह है कि एक स्टेटस बार है जिसमें दिखाया गया है कि काम का प्रतिशत अब तक पूरा हो चुका है, जैसे नीचे दिया गया है।

Application.StatusBar क्या है?

Application.StatusBar वह संपत्ति है जिसका उपयोग हम मैक्रो कोडिंग में कर सकते हैं स्थिति दिखाने के लिए जब मैक्रो दृश्य के पीछे चल रहा है।

यह हमारे "VBA प्रोग्रेस बार" जितना सुंदर नहीं है, लेकिन मैक्रो प्रोजेक्ट की स्थिति जानने के लिए काफी अच्छा है।

VBA का उपयोग करके स्टेटसबार बनाने का उदाहरण

स्टेटस बार बनाने के लिए नीचे दिए गए चरणों का पालन करें।

चरण 1: सबसे पहले, वर्कशीट में अंतिम प्रयुक्त पंक्ति को खोजने के लिए VBA वैरिएबल को परिभाषित करें।

कोड:

उप स्थिति_Bar_Progress () लंबी अवधि के उप के रूप में मंद LR

चरण 2: नीचे दिए गए कोड का उपयोग करके अंतिम प्रयुक्त पंक्ति का पता लगाएं।

कोड:

Sub Status_Bar_Progress () Dim LR अस लॉन्ग LR = सेल्स (Rows.Count, 1) .और (xlUp) .Row सब उप।

चरण 3: अगला, हमें प्रदर्शित होने वाली सलाखों की संख्या को रखने के लिए चर को परिभाषित करना होगा।

कोड:

Sub Status_Bar_Progress () Dim LR अस लॉन्ग LR = सेल्स (Rows.Count, 1) .और (xlUp) ।Row Dim NumOfBars इंटेगर एंड सब के रूप में।

इससे पता चलेगा कि स्टेटस बार में कितने बार दिखाने की अनुमति है।

चरण 4: इस चर के लिए, बार की सीमा को 45 के रूप में संग्रहीत करें।

कोड:

Sub Status_Bar_Progress () मंद LR अस लॉन्ग LR = सेल्स (Rows.Count, 1) .End (xlUp) .Row Dim NumOfBars Integer NumOfBars = 45 End Sub के रूप में।

चरण 5: मैक्रो चालू होने पर वर्तमान स्थिति और प्रतिशत पूरा करने के लिए दो और चर को परिभाषित करें।

कोड:

Sub Status_Bar_Progress () Dim LR अस लॉन्ग LR = सेल्स (Rows.Count, 1) .End (xlUp) ।Row Dim NumOfBars Integer NumOfBars के रूप में = 45 Dim Presentationatus के रूप में Integer Dim PercetageCompleted Integer End Sub Sub के रूप में।

चरण 6: अब, स्थिति पट्टी को सक्षम करने के लिए, नीचे दिए गए कोड का उपयोग करें।

कोड:

Sub Status_Bar_Progress () Dim LR As Long LR = Cell (Rows.Count, 1) .End (xlUp) .Row Dim NumOfBars Integer NumOfBars = 45 Dim Presentationatus के रूप में Integer Dim PercetageCompleted As Integer Application.StatusBar = "Space" NumOfBars) & ")" एंड सब

यह क्या करेगा यह ब्रैकेट () को जोड़ेगा और समापन ब्रैकेट ()) के साथ पाठ समाप्त होने से पहले 45 रिक्त स्थान वर्ण जोड़ेगा।

कोड निष्पादित करें, और हम नीचे एक्सेल VBA स्टेटस बार में देख सकते हैं।

आउटपुट:

चरण 7: अब, हमें मैक्रो के प्रतिशत की गणना करने के लिए VBA में फॉर नेक्स्ट लूप को शामिल करना होगा। मैक्रो शुरू करने के लिए एक चर को परिभाषित करें।

कोड:

Sub Status_Bar_Progress () Dim LR As Long LR = Cell (Rows.Count, 1) .End (xlUp) .Row Dim NumOfBars Integer NumOfBars = 45 Dim Presentationatus के रूप में Integer Dim PercetageCompleted As Integer Application.StatusBar = "Space" NumOfBars) & ")" डिम के अस लॉन्ग फॉर के = 1 टू एलआर नेक्स्ट के एंड सब उप

चरण 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

We have used the “INT” function to get the integer value as a result.

Step 9: Now, we need to calculate what the “Percentage Completion” is, so we can apply the formula as shown 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) 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

याद रखने वाली चीज़ें

  • हम केवल उन कार्यों को जोड़ सकते हैं जिन्हें लूप के भीतर करने की आवश्यकता है।
  • आप "ईवेंट्स" प्रक्रिया को जोड़ने के बाद उन कार्यों को जोड़ सकते हैं जो आपको करने की आवश्यकता है।

दिलचस्प लेख...