VBA IsEmpty - Ako používať funkciu VBA IsEmpty? (Príklady)

IsEmpty je funkcia pracovného hárka, ktorá sa používa na zistenie, či sú dané odkazy na bunky alebo rozsah buniek prázdne alebo nie, pretože ide o funkciu hárka, takže na použitie vo VBA používame aplikáciu. Metóda listu vo VBA na použitie tejto funkcie, táto funkcia spadá pod logické zoznamy funkcií a vráti true, ak je odkaz prázdny.

Funkcia VBA IsEmpty

VBA IsEmpty je logická funkcia, ktorá testuje, či je vybraný prázdny alebo nie. Pretože je to logická funkcia, vráti výsledky v boolovských hodnotách, tj. TRUE alebo FALSE.

Ak je vybratá bunka prázdna, vráti hodnotu TRUE alebo inak vráti hodnotu FALSE.

V tomto článku vám ukážeme, ako používať funkciu „ISEMPTY“ vo VBA na kontrolu buniek pomocou kódov VBA.

Čo robí funkcia ISEMPTY vo VBA?

Prázdne bunky nás často frustrujú, aby sme v pracovnom hárku pracovali efektívne. Nájsť prázdne bunky nie je najťažšie, ale ak ich prázdne bunky skrývajú uprostred údajov, je potrebné ich nájsť.

Na nájdenie prázdnych buniek v programe Excel máme funkciu nazvanú „ISBLANK“ ako funkcia pracovného hárka, ale vo VBA sa nazýva „ISEMPTY“.

Funguje to podobne ako funkcia listu „ISBLANK“. Teraz sa pozrite na nasledujúci vzorec funkcie „ISEMPTY“.

Ako vidíme na obrázku vyššie, vráti výsledok ako Boolean, tj TRUE alebo FALSE.

Príklady funkcie ISEMPTY vo VBA

Nasledujú príklady IsEmpty vo VBA.

Príklad č

Teraz uvidíme prvý praktický príklad „ISEMPTY“. Z tohto dôvodu si pozrite nasledujúci obrázok tabuľky.

Teraz na testovanie všetkých týchto funkcií použijeme funkciu Excel VBA ISEMPTY.

Krok 1: Definujte premennú ako boolovskú .

Kód:

Sub IsEmpty_Example1 () Dim K ako boolovský koncový sub

Krok 2: Priraďte tejto premennej hodnotu pomocou funkcie VBA ISEMPTY .

Kód:

Sub IsEmpty_Example1 () Dim K As Boolean K = IsEmpty (End Sub

Krok 3: Výraz nie je nič iné, len to, čo je bunka, ktorú testujeme. Teraz testujeme bunku A1 bunku .

Kód:

Sub IsEmpty_Example1 () Dim K As Boolean K = IsEmpty (Range ("A1"). Value) End Sub

Krok 4: Zobrazte hodnotu tejto premennej v priečinku VBA Msgbox .

Kód:

Sub IsEmpty_Example1 () Dim K As Boolean K = IsEmpty (Range ("A1"). Value) MsgBox K End Sub

Spustením tohto kódu skontrolujete výsledok.

Pretože v bunke A1 je hodnota, dostali sme výsledok ako FALSE.

Teraz zmením odkaz na bunku z A1 na A5.

Kód:

Sub IsEmpty_Example1 () Dim K As Boolean K = IsEmpty (Range ("A5"). Value) MsgBox K End Sub

Spustením tohto kódu zobrazíte výsledok.

Výsledok sme dostali ako PRAVDA, odkazovaná bunka A5 je vlastne prázdna bunka, takže sme dostali výsledok ako „PRAVDA“.

Teraz otestujem bunku A8.

Kód:

Sub IsEmpty_Example1 () Dim K As Boolean K = IsEmpty (Range ("A8"). Value) MsgBox K End Sub

Spustením tohto kódu zobrazíte výsledok.

Aha !!! Počkaj…

Výsledok sme dostali ako FALSE, aj keď v bunke A8 nie je žiadna hodnota.

Otázkou teraz je, že ide o chybný výsledok vzorca „ISEMPTY“ ?.

Nie … absolútne nie !!!

When I tried examining the cell A8 actually there is a space character inside the cell which is not easy to see with bare eyes.

So the conclusion is even Space is considered as a character in excel and VBA language.

Example #2 - Combination of VBA ISEMPTY with IF Condition

Actually, the real usage of the function “ISEMPTY” is admirable when we use it with other logical functions.

Especially when we use it with IF condition we can derive many useful results from it.

For this demonstration take a look at the below example.

In the Status column, if the “PF Status” column is empty, we need the value as “No Update,” and if there is any value, we need the values as “Collected Updates.”

Remember here we don’t need the default result of TRUE or FALSE. We need our own results here, to have our own results we need to use Excel VBA ISEMPTY with IF condition.

Step 1: Open IF condition.

Code:

Sub IsEmpty_Example2() If End Sub

Step 2: Inside the IF condition open ISEMPTY function.

Code:

Sub IsEmpty_Example2() If IsEmpty( End Sub

Step 3: The first logical test is cell B2 value is empty or not.

Code:

Sub IsEmpty_Example2() If IsEmpty(Range("B2").Value) Then End Sub

Step 4: If the logical test in excel vba is TRUE i.e., if the cell is empty, we need the result as “No Update” in cell C2.

Code:

Sub IsEmpty_Example2() If IsEmpty(Range("B2").Value) Then Range("C2").Value = "No Update" End Sub

Step 5: If the logical test is FALSE, we need the result in cell C2 as “Collected Updates.”

Code:

Sub IsEmpty_Example2() If IsEmpty(Range("B2").Value) Then Range("C2").Value = "No Update" Else Range("C2").Value = "Collects Updates" End If End Sub

Ok, we are done.

Run the code to get the result.

We got the result as “Collected Updates” because we have the non-empty cell in B2.

Now similarly apply the code for other cells to test.

Code:

Sub IsEmpty_Example2() If IsEmpty(Range("B2").Value) Then Range("C2").Value = "No Update" Else Range("C2").Value = "Collects Updates" End If If IsEmpty(Range("B3").Value) Then Range("C3").Value = "No Update" Else Range("C3").Value = "Collected Updates" End If If IsEmpty(Range("B4").Value) Then Range("C4").Value = "No Update" Else Range("C4").Value = "Collected Updates" End If End Sub

Run this code to have the results.

In cell C3 we got the result as “No Update” because there is no value in cell B3 i.e. Empty Cell. Since the logical formula returned TRUE we got the respective result.

Example #3 - Alternative to VBA ISEMPTY Function

Máme alternatívu k funkcii ISEMPTY, bez použitia funkcie Excel VBA ISEMPTY môžeme bunku skutočne otestovať.

Napríklad si pozrite nasledujúci kód.

Kód:

Sub IsEmpty_Example3 () If Range ("B2"). Value = "" Then Range ("C2"). Value = "No Update" Else Range ("C2"). Value = "Collected Updates" End If End Sub

Riadok kódu Range (“B2 ″). Hodnota =” ” znamená, či je bunka B2 bunka prázdna alebo nie.

Dvojité úvodzovky („“) predstavujú prázdnu bunku alebo nie, ak je prázdny výsledok TRUE alebo inak FALSE.

Zaujímavé články...