VBA-RÄKNING
Kriteriebaserade funktioner är linjalerna för excel i beräkningar. I början av inlärningen av excel måste vi ha lärt oss COUTNIF-processen i Excel. I våra tidigare artiklar har vi visat dig hur du arbetar med COUNTIF-funktionen i Excel VBA.
Se vår artikel om COUNTIF-formel i Excel för att lära känna grunderna för COUNTIF-funktionen i Excel VBA. I den här artikeln visar vi dig hur du använder samma funktion i VBA-kodning. Nu kommer vi att se samma formel i VBA. Först och främst är COUNTIF inte en VBA-funktion; istället är det en kalkylbladfunktion som kan nås under kalkylbladets funktionsklass.

Exempel på Excel VBA Countif-funktion
Okej, låt oss se det enkla exemplet.
Titta på nedanstående exempel på att räkna värden från partiet.

I bilden ovan har vi stadsnamn från cell A1 till A10. I cell C3 måste vi räkna hur många gånger stadsnamnet ”Bangalore” visas i området A1 till A10.
OK, följ stegen nedan för att skriva koden för att tillämpa COUNTIF-funktionen.
Steg 1: Starta subproceduren.
Koda:
Alternativ Explicit Sub Countif_Example1 () End Sub

Steg 2: Eftersom vi måste lagra resultatet i cell C3 startar du Range ("C3").
Koda:
Sub Countif_Example1 () Range ("C3"). Värde = End Sub

Steg 3: I cell C3 försöker vi nå resultatet genom att använda excel VBA COUNTIF-funktionen. Så för att komma åt funktionen måste vi först använda klassen Arbetsbladets funktion.
Koda:
Sub Countif_Example1 () Range ("C3"). Value = WorksheetFunction. Avsluta sub

Steg 4: Från den förlorade välj excel VBA COUNTIF-funktionen.
Koda:
Sub Countif_Example1 () Range ("C3"). Value = WorksheetFunction.CountIf (End Sub

Steg 5: Om du tittar på parametrarna för VBA COUNTIF-funktionen ser vi inte parametern, som vi ser i kalkylbladet.

Som vi kan se i bilden ovan i kalkylbladet har vi exakt syntax, men i VBA kan vi bara se Arg 1 & Arg 2.
Arg 1 är intervall, så välj intervallet som A1 till A10.
Koda:
Sub Countif_Example1 () Range ("C3"). Value = WorksheetFunction.CountIf (Range ("A1: A10"), End Sub

Steg 6: Arg 2 är vilket värde vi behöver räkna från intervallet A1 till A10. I det här exemplet måste vi beräkna "Bangalore."
Koda:
Sub Countif_Example1 () Range ("C3"). Value = WorksheetFunction.CountIf (Range ("A1: A10"), "Bangalore") End Sub

Okej, vi är klara.
Kör koden för att se resultatet i cell C3.

Vi fick resultatet som 4. Eftersom stadsnamnet ”Bangalore” som visas i cell A1, A4, A7 och A10 VBA COUNTIF-funktionen returnerade produkten som 4.
If you can see VBA code has returned only the result of the formula, we don’t get to know the procedure in the formula bar.

To arrive at the formula, we need to write the code slightly differently. Below is the code for you to apply the formula itself to the cell.
Code:
Sub Countif_Example1() Range("C3").Formula = "=CountIf(A1:A10, ""Bangalore"")" End Sub
This will apply the formula to the cell C3.

Arrive Result with Variables
Variables are an integral part of any coding language. We need to declare variables to work efficiently with the VBA code. For example, look at the below code.
Code:
Sub Countif_Example2() Dim ValuesRange As Range Dim ResultCell As Range Dim CriteriaValue As String Set ValuesRange = Range("A1:A10") Set ResultCell = Range("C3") CriteriaValue = "Bangalore" ResultCell = WorksheetFunction.CountIf(ValuesRange, CriteriaValue) End Sub
Let me decode the code for you to understand better.
Firstly I have declared the two variables as Range.
Dim ValuesRange As Range: This is to reference the list of values.
Dim ResultCell As Range: This to reference the result cell.
Then I have set the range of references to both the variables.
Set ValuesRange = Range(“A1: A10”): This is the range where all the city names are there.
Set ResultCell = Range(“C3”): In this cell, we will store the result of the COUNTIF function.
In the meantime, I have declared one more variable to store the criteria value.
Dim CriteriaValue As String
CriteriaValue = “Bangalore”
Så nu har variabeln "CriteteriaValue" värdet "Bangalore."
I nästa rad, som vanligt, har jag tillämpat COUTNIF-funktionen.
ResultCell = WorksheetFunction.CountIf (ValuesRange, CriteriaValue)
Så här kan vi använda COUNTIF-funktionen i Excel VBA för att passa våra behov.