VBA OR-functie (voorbeelden) - Hoe OR Logical Operator in VBA te gebruiken?

Of is een logische functie in een van de programmeertalen en vergelijkbaar in VBA hebben we de OR-functie, omdat het een logische functie is, is het resultaat van deze functie waar of onwaar, deze functie wordt gebruikt voor twee of veel voorwaarden samen en geeft us true resultaat wanneer een van de voorwaarden waar wordt geretourneerd.

Wat is de OF-functie in VBA?

In Excel vormen logische functies het hart van de formules die we dagelijks gebruiken. Logische functies zijn er om de logische test uit te voeren en geven resultaat in Booleaans gegevenstype, dwz WAAR of ONWAAR. Enkele van de logische formules in Excel zijn "IF, IFERROR in Excel, ISFOUT in Excel, AND en OR Excel-functie." Ik hoop dat je ze vrij vaak hebt gebruikt als werkbladfunctie. Ook in VBA kunnen we ze allemaal gebruiken, en in dit artikel leggen we u uit hoe u de functie "VBA OR" kunt gebruiken.

Wat is het eerste dat in je opkomt als je aan het woord "OF" denkt?

In eenvoudige bewoordingen betekent ' OF' 'dit of dat'

Met hetzelfde idee is OR een logische functie die het resultaat TRUE geeft als een van de logische tests WAAR is en ONWAAR als het resultaat geeft als geen van de logische tests WAAR is.

Dit werkt precies het tegenovergestelde van de VBA AND-functie. EN-functie retourneert alleen WAAR als alle logische voorwaarden WAAR zijn. Als aan een van de voorwaarden niet wordt voldaan, krijgen we als resultaat FALSE.

De formule van VBA OR-functie

Ik zal een syntaxis voor u opstellen om de functie te begrijpen.

(Logische test) OF (Logische test) OF (Logische test)

Eerst moeten we vermelden wat de logische test is, dan het woord OR noemen en vervolgens vermelden wat de tweede logische test is. Als u een meer logische test wilt uitvoeren, noem dan het woord OF na een logische test.

Van alle logische tests die u doet, als een van de tests tevreden of waar is, krijgen we het resultaat als WAAR als er geen of tevreden is, dan is het resultaat ONWAAR.

Voorbeelden van het gebruik van de OR-functie in VBA

We laten u een eenvoudig voorbeeld zien van het gebruik van de OR-functie in VBA.

Om de logische VBA-functie te begrijpen OF laat me u een voorbeeld geven. Stel dat we de logische test willen uitvoeren of het getal 25 groter is dan 20 of dat getal 50 kleiner is dan 30.

Stap 1: Maak een macronaam.

Stap 2: Definieer de variabele als een string.

Code:

Sub OR_Example1 () Dim i As String End Sub

Stap 3: Nu zullen we voor deze variabele de waarde toewijzen via de OF logische test.

Code:

Sub OR_Example1 () Dim i As String i = End Sub

Stap 4: Onze eerste logische test is 25> 20 .

Code:

Sub OR_Example1 () Dim i As String i = 25> 20 End Sub

Stap 5: Noem nu, na de eerste logische test, het woord OR en voer de tweede logische test in.

Code:

Sub OR_Example1 () Dim i As String i = 25> 20 Of 50 <30 End Sub

Stap 6: Ok, nu test de VBA OR-functie of de logische tests WAAR of ONWAAR zijn. Wijs nu het resultaat van de variabele toe aan het VBA- berichtvenster .

Code:

Sub OR_Example1 () Dim i As String i = 25> 20 Or 50 <30 MsgBox i End Sub

Stap 7: Voer de macro uit en wat het resultaat is.

We hebben het resultaat als WAAR gekregen omdat van de twee logische tests die we hebben geleverd, één test WAAR is, dus het resultaat is WAAR.

25 is groter dan 20 en 50 is niet minder dan 30. In dit geval is de eerste logische test WAAR, maar de tweede is ONWAAR. Omdat we de VBA OR-functie hebben toegepast, moet een van de voorwaarden WAAR zijn om het resultaat als WAAR te krijgen.

Kijk nu naar de onderstaande code.

Code:

Sub OR_Example1 () Dim i As String i = 25 = 20 Of 50 = 30 MsgBox i End Sub

Ik heb de logische testvergelijkingen gewijzigd van> en <naar gelijk (=) teken. Dit levert FALSE op als resultaat, omdat 25 niet gelijk is aan 20 en 50 niet gelijk is aan 30.

VBA OR-functie met IF-voorwaarde is krachtig

Zoals ik al zei, OR kan als resultaat WAAR of ONWAAR retourneren, maar met de andere logische functie "ALS" kunnen we resultaten manipuleren volgens onze behoeften.

Voer dezelfde logische tests uit als hierboven, OF heeft alleen WAAR of ONWAAR geretourneerd, maar laten we dit OF combineren met ALS.

Stap 1: Open de functie IF voordat u een test uitvoert .

Code:

Sub OR_Example2 () Dim i As String IF End Sub

Stap 2: Voer nu tests uit met de OR- functie.

Code:

Sub OR_Example2 () Dim i As String IF 25 = 20 Of 50 = 30 End Sub

Step 3: Put the word “Then” and write the result. If the condition is TRUE, assign the value to the variable as “Condition is Satisfied.”

Code:

Sub OR_Example2() Dim i As String If 25 = 20 Or 50 = 30 Then i = "Condition is Satisfied" End Sub

Step 4: If the condition is FALSE, then we need a different result, so put the word “ELSE” and, in the next line, assign the value to the variable “what should be the result if the condition or logical test is FALSE.”

Code:

Sub OR_Example2() Dim i As String If 25 = 20 Or 50 = 30 Then i = "Condition is Satisfied" Else i = "Condition is not Satisfied" End Sub

Step 5: End the IF function with the word “End If.”

Code:

Sub OR_Example2() Dim i As String If 25 = 20 Or 50 = 30 Then i = "Condition is Satisfied" Else i = "Condition is not Satisfied" End If End Sub

Step 6: Assign the value of the variable result to the message box.

Code:

Sub OR_Example2() Dim i As String If 25 = 20 Or 50 = 30 Then i = "Condition is Satisfied" Else i = "Condition is not Satisfied" End If MsgBox i End Sub

Run the macro, if the logical test is TRUE, we will get the result as “Condition is Satisfied,” or else we will get “Condition is not Satisfied.”

We got the result as “Condition is not Satisfied” because both the logical tests are FALSE.

Now I will change the logical tests.

Code:

Sub OR_Example2() Dim i As String If 25> 20 Or 50 < 30 Then i = "Condition is Satisfied" Else i = "Condition is not Satisfied" End If MsgBox i End Sub

I will run the macro and see what the result is.

Like this, we can use one logical function with other logical functions to arrive at the results.

Solve the below case study to get used to logical functions.

Case Study to Solve

I have employee names and their respective departments.

If you have tried and not found the result, then you can refer below code to understand the logic.

Code:

Sub Bonus_Calculation() Dim i As Long For i = 2 To 10 If Cells(i, 2).Value = "Finance" Or Cells(i, 2).Value = "IT" Then Cells(i, 3).Value = 5000 Else Cells(i, 3).Value = 1000 End If Next i End Sub

Als de werknemer afkomstig is van "Finance" of "IT", dan zou hij de bonus als "5000" moeten krijgen. Voor andere afdelingsmedewerkers is de bonus “1000”.

Voer de logische test uit en kom tot de resultaten.

Interessante artikelen...