VBA काउंटर - एक्सेल VBA में काउंटर कैसे बनाएं? (उदाहरण सहित)

विषय - सूची

एक्सेल VBA काउंटर

मूल्यों को गिनने के लिए MS Excel में विभिन्न कार्य हैं, चाहे वह एक स्ट्रिंग हो, संख्या हो। मतगणना कुछ मानदंडों के आधार पर की जा सकती है। एक्सेल में COUNT, COUNTA, COUNTBLANK, COUNTIF और COUNTIFS शामिल हैं। हालाँकि, ये फ़ंक्शन कुछ कार्य नहीं कर सकते हैं जैसे कि उनके रंग के आधार पर कोशिकाओं को गिनना, केवल बोल्ड मानों को गिनना, आदि। इसीलिए हम VBA में एक काउंटर बनाएंगे ताकि हम एक्सेल में इस प्रकार के कार्यों के लिए गणना कर सकें।

आइए हम एक्सेल VBA में कुछ काउंटर बनाएं।

एक्सेल VBA काउंटर के उदाहरण

नीचे VBA में काउंटर के उदाहरण दिए गए हैं।

उदाहरण 1

मान लें कि हमारे पास 32 पंक्तियों के लिए ऊपर जैसा डेटा है। हम एक VBA काउंटर बनाएंगे, जो मानों की गिनती करेगा, जो मानों को गिनने के लिए 50 से अधिक और एक काउंटर हैं, जो 50 से कम हैं। हम VBA कोड इस तरह बनाएंगे, ताकि उपयोगकर्ता के लिए डेटा हो सके एक्सेल में असीमित पंक्तियाँ।

ऐसा करने के लिए, चरण होंगे:

सुनिश्चित करें कि डेवलपर टैब एक्सेल दिखाई दे रहा है। टैब को दृश्यमान बनाने के लिए (यदि नहीं), तो चरण हैं:

रिबन में 'फ़ाइल' टैब पर क्लिक करें और सूची से 'विकल्प' चुनें ।

सूची से ' कस्टमाइज़ रिबन' चुनें , 'डेवलपर' के लिए बॉक्स की जाँच करें और ठीक पर क्लिक करें ।

अब 'डेवलपर' टैब दिखाई देता है।

कमांड बटन को 'डेवलपर' टैब में 'नियंत्रण' समूह में उपलब्ध 'इन्सर्ट' कमांड का उपयोग करके डालें

ALT कुंजी दबाते समय , माउस के साथ कमांड बटन बनाएं। यदि हम एएलटी कुंजी दबाते रहते हैं , तो कमांड बटन के किनारे कोशिकाओं की सीमा के साथ स्वचालित रूप से चले जाते हैं।

प्रासंगिक मेनू खोलने के लिए कमांड बटन पर राइट-क्लिक करें (सुनिश्चित करें कि 'डिज़ाइन मोड' सक्रिय है, अन्यथा, हम प्रासंगिक मेनू को खोलने में सक्षम नहीं होंगे)।

मेनू से 'गुण' चुनें ।

कमांड बटन के गुणों को बदलें, जैसे, नाम, शीर्षक, और फ़ॉन्ट, आदि।

फिर से राइट-क्लिक करें और संदर्भ मेनू से 'कोड देखें' चुनें।

Visual Basic Editor अभी खोला गया है, और डिफ़ॉल्ट रूप से, कमांड बटन के लिए एक सबरूटीन पहले से ही बनाया गया है।

हम अब कोड लिखेंगे। हम 3 चर घोषित करेंगे। लूप उद्देश्य के लिए एक, गिनती करने के लिए एक, और अंतिम पंक्ति के लिए मूल्य को संग्रहीत करने के लिए एक।

हम सेल A1 को चुनने के लिए कोड का उपयोग करेंगे और फिर सेल A1 के वर्तमान क्षेत्र को और फिर अंतिम भरे हुए पंक्ति को अंतिम भरे हुए नंबर को प्राप्त करने के लिए नीचे उतरेंगे।

हम A2 सेल में A कॉलम में अंतिम भरे गए सेल में लिखे गए मानों की जांच करने के लिए VBA में 'for' लूप चलाएंगे । हम 'काउंटर' वैरिएबल के मान को 1 से बढ़ा देंगे यदि मान 50 से अधिक है और सेल के फ़ॉन्ट रंग को 'ब्लू' में बदल देगा , और यदि मान 50 से कम है, तो सेल का फ़ॉन्ट रंग 'रेड' होगा

जांच और गिनती के बाद, हमें मूल्यों को प्रदर्शित करने की आवश्यकता है। ऐसा ही करने के लिए, हम 'VBA MsgBox' का उपयोग करेंगे

कोड:

निजी सब काउंटिंग काउंसिलबिल्व्यू_क्लिक () डिम i, काउंटर ऐज एंगर डिम लास्ट एरो लॉन्ग एरो = रेंज ("ए 1")। करेंट रीरेजियन.एंड (xlDown) .Row i के लिए 2: यदि सेल (i, 1) को एरो करने के लिए (i, 1) .Value> 50 तब 50। counter = counter + 1 Cells (i, 1) .Font.ColorIndex = 5 Else Cells (i, 1)। & _ vbCL

'डिज़ाइन मोड' को निष्क्रिय करें और 'कमांड बटन' पर क्लिक करें। परिणाम इस प्रकार होगा।

उदाहरण # 2

मान लीजिए कि हम एक्सेल VBA का उपयोग करके समय काउंटर बनाना चाहते हैं:

If we click on the ‘Start’ button, the timer starts, and if we click on the ‘Stop’ button, the timer stops.

To do the same, steps would be:

Create a format like this in an excel sheet.

Change the format of the cell A2 as ‘hh:mm: ss.’

Merge the cells C3 to G7 by using the Merge and Center Excel command in the ‘Alignment’ group in the ‘Home’ tab.

Give the reference of cell A2 for just merged cell and then do the formatting like make the font style to ‘Baskerville,’ font size to 60, etc.

Create two command buttons, ‘Start’ and ‘Stop’ using the ‘Insert’ command available in the ‘Controls’ group in the ‘Developer’ tab.

Using the ‘Properties’ command available in the ‘Controls’ group in the ‘Developer’ tab, change the properties.

Select the commands buttons one by one and choose the ‘View Code’ command from the ‘Controls’ group in the ‘Developer’ tab to write the code as follows.

Choose from the drop-down the appropriate command button.

Insert a module into ‘ThisWorkbook‘ by right-clicking on the ‘Thisworkbook’ and then choose ‘Insert’ and then ‘Module.’

Write the following code in the module.

Code:

Sub start_time() Application.OnTime Now + TimeValue("00:00:01"), "next_moment" End Sub Sub end_time() Application.OnTime Now + TimeValue("00:00:01"), "next_moment", , False End Sub Sub next_moment() If Worksheets("Time Counter").Range("A2").Value = 0 Then Exit Sub Worksheets("Time Counter").Range("A2").Value = Worksheets("Time Counter").Range("A2").Value - TimeValue("00:00:01") start_time End Sub

We have used the ‘onTime‘ method of the Application object, which is used to run a procedure at a scheduled time. The procedure, which we have scheduled to run, is “next_moment.”

Save the code. Write the time in the A2 cell and click on the ‘Start’ button to start the time counter.

Example #3

Suppose we have a list of students along with marks scored by them. We want to count the number of students who passed and who failed.

To do the same, we will write the VBA code.

Steps would be:

Open Visual Basic editor by pressing shortcut in excel Alt+F11 and double click on ‘Sheet3 (Counting Number of students)’ to insert a subroutine based on an event in Sheet3.

Choose ‘Worksheet’ from the dropdown.

As we pick ‘Worksheet’ from the list, we can see, there are various events in the adjacent dropdown. We need to choose ‘SelectionChange’ from the list.

We will declare the VBA variable ‘lastrow’ for storing last row number as a list for students can increase, ‘pass’ to store a number of students who passed, and ‘fail’ to store a number of students who failed.

We will store the value of the last row number in ‘lastrow.’

We will create the ‘for’ loop for counting based on condition.

We have set the condition if the total marks are greater than 99, then add the value 1 to the ‘pass’ variable and add one value to the ‘fail’ variable if the condition fails.

The last statement makes the heading ‘Summary’ bold.

To print the values in the sheet, the code would be:

Code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim lastrow As Long Dim pass As Integer Dim fail As Integer lastrow = Range("A1").CurrentRegion.End(xlDown).Row For i = 2 To lastrow If Cells(i, 5)> 99 Then pass = pass + 1 Else fail = fail + 1 End If Cells(1, 7).Font.Bold = True Next i Range("G1").Value = "Summary" Range("G2").Value = "The number of students who passed is " & pass Range("G3").Value = "The number of students who failed is " & fail End Sub

Now whenever there is a change in selection, values will be calculated again as below:

Things to Remember

  1. Save the file after writing code in VBA with .xlsm excel extension; otherwise, the macro will not work.
  2. Use the ‘For’ loop when it is decided already for how many times the code in the VBA loop will run.

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