VBA TAI -toiminto (esimerkkejä) Kuinka käyttää TAI loogista operaattoria VBA: ssa?

Tai on looginen funktio millä tahansa ohjelmointikielellä ja vastaava VBA: lla, meillä on TAI-funktio, koska se on looginen funktio, jonka tämän funktion antama tulos on joko tosi tai väärä, tätä funktiota käytetään kahteen tai useaan ehtoon yhdessä ja antaa meille tosi tulos, kun jompikumpi ehdoista palautetaan totta.

Mikä on TAI-toiminto VBA: ssa?

Excelissä loogiset toiminnot ovat päivittäin käyttämiemme kaavojen ydin. Loogiset toiminnot ovat loogisen testin suorittamiseen ja antavat tuloksen Boolen tietotyypiksi, eli joko TOSI tai EPÄTOSI. Jotkut Excelin loogisista kaavoista ovat ”IF, IFERROR excelissä, ISERROR excelissä JA OR OR excel -toiminnot”. Toivon, että olet käyttänyt niitä melko usein laskentataulukko-funktiona. Myös VBA: ssa voimme käyttää niitä kaikkia, ja tässä artikkelissa selitämme sinulle tapoja käyttää "VBA OR" -toimintoa.

Mikä tulee ensin mieleesi, kun ajattelet sanaa "TAI"?

Yksinkertaisesti sanottuna "OR" tarkoittaa "joko tätä tai toista"

Samalla ajatuksella OR on looginen toiminto, joka antaa tuloksen TOSI, jos jokin loogisista testeistä on TOSI, ja antaa tulokseksi EPÄTOSI, jos mikään loogisista testeistä ei ole TOSI.

Tämä toimii täsmälleen päinvastoin kuin VBA AND -toiminto. AND-funktio palauttaa TOSI vain, jos kaikki loogiset ehdot ovat TOSI. Jos joku ehdoista ei täyty, saamme sen seurauksena EPÄTOSI.

VBA: n tai funktion kaava

Haluan kehittää syntaksin, jotta ymmärrät toiminnon.

(Looginen testi) TAI (Looginen testi) TAI (Looginen testi)

Ensinnäkin meidän on mainittava, mikä on looginen testi, sitten mainittava sana OR ja mainittava sitten toinen looginen testi. Jos haluat suorittaa loogisemman testin, mainitse sitten sana TAI loogisen testin suorittamisen jälkeen.

Kaikista tekemistäsi loogisista testeistä, jos joku testeistä on tyydyttävä tai totta, saamme tuloksen TOSI, jos ei yhtään tai tyydyttävää, niin tulos on EPÄTOSI.

Esimerkkejä OR-toiminnon käytöstä VBA: ssa

Näytämme sinulle yksinkertaisen esimerkin OR-toiminnon käytöstä VBA: ssa.

Ymmärtääkseni loogisen VBA-toiminnon TAI anna minun antaa sinulle esimerkki. Oletetaan, että haluamme suorittaa loogisen testin, onko luku 25 suurempi kuin 20 vai numero 50 alle 30.

Vaihe 1: Luo makron nimi.

Vaihe 2: Määritä muuttuja merkkijonoksi.

Koodi:

Sub OR_Example1 () Dim i String End Sub

Vaihe 3: Nyt tälle muuttujalle annamme arvon OR-loogisen testin avulla.

Koodi:

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

Vaihe 4: Ensimmäinen looginen testi on 25> 20 .

Koodi:

Sub OR_Example1 () Dim i merkkijonona i = 25> 20 End Sub

Vaihe 5: Mainitse nyt ensimmäisen loogisen testin jälkeen sana TAI ja kirjoita toinen looginen testi.

Koodi:

Sub OR_Example1 () Dim i merkkijonona i = 25> 20 tai 50 <30 End Sub

Vaihe 6: Ok, nyt VBA OR -toiminto testaa, ovatko loogiset testit TOSI vai EPÄTOSI. Määritä nyt muuttujan tulos VBA- viestiruutuun .

Koodi:

Sub OR_Example1 () Dim i merkkijonona i = 25> 20 tai 50 <30 MsgBox i End Sub

Vaihe 7: Suorita makro ja mitä tulos on.

Saimme tuloksen TOSI, koska kahdesta toimittamastamme loogisesta testistä yksi testi on TOSI, joten tulos on TOSI.

25 on suurempi kuin 20 ja 50 on vähintään 30. Tällöin ensimmäinen looginen testi on TOSI, mutta toinen on EPÄTOSI. Koska olemme käyttäneet VBA OR -funktiota, se tarvitsee minkä tahansa ehdoista, jotta se olisi TOSI saadakseen tuloksen TOSI.

Katso nyt alla oleva koodi.

Koodi:

Sub OR_Example1 () Dim i merkkijonona i = 25 = 20 Tai 50 = 30 MsgBox i End Sub

I have changes the logical test equations from> and < to equal (=) sign. This will return FALSE as the result because 25 is not equal to 20 and 50 is not equal to 30.

VBA OR Function With IF Condition is Powerful

As I told, OR can return either TRUE or FALSE as a result, but with the other logical function “IF,” we can manipulate results as per our needs.

Take the same logical tests from above, OR has returned only TRUE or FALSE but let’s combine this OR with IF.

Step 1: Before conducting any test, open the function IF.

Code:

Sub OR_Example2() Dim i As String IF End Sub

Step 2: Now, conduct tests using the OR function.

Code:

Sub OR_Example2() Dim i As String IF 25 = 20 Or 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

Jos työntekijä on "Finance" tai "IT", heidän pitäisi saada bonus "5000". Muille osaston työntekijöille bonus on “1000”.

Suorita looginen testi ja pääse tuloksiin.

Mielenkiintoisia artikkeleita...