VBA IsEmpty - Cum se folosește funcția VBA IsEmpty? (Exemple)

IsEmpty este o funcție de foaie de lucru care este utilizată pentru a afla dacă o anumită referință de celulă sau o gamă de celule sunt goale sau nu, deoarece este o funcție de foaie de lucru, astfel încât să o folosim în VBA folosim aplicația. Metoda foii de lucru în VBA pentru a utiliza această funcție, această funcție intră sub listele logice de funcții și returnează adevărat dacă referința este goală.

Funcția VBA IsEmpty

VBA IsEmpty este o funcție logică care testează dacă este selectat gol sau nu. Deoarece este o funcție logică, va returna rezultatele în valori booleene, adică fie ADEVĂR, fie FALS.

Dacă celula selectată este goală va reveni la ADEVĂRAT sau altfel va reveni la FALS.

În acest articol, vă vom arăta cum să utilizați funcția „ISEMPTY” în VBA pentru a verifica celulele folosind coduri VBA.

Ce face funcția ISEMPTY în VBA?

De multe ori celulele goale ne frustrează să lucrăm eficient în foaia de lucru. Găsirea celulelor goale nu este cea mai grea, dar dacă celulele goale le ascund în mijlocul datelor, atunci este nevoie de o taxă pentru a le găsi.

Pentru a găsi celulele goale în Excel avem funcția numită „ISBLANK” ca funcție de foaie de lucru, dar în VBA se numește „ISEMPTY”.

Acest lucru funcționează similar cu funcția de foaie de lucru „ISBLANK”. Acum aruncați o privire la formula de mai jos a funcției „ISEMPTY”.

După cum putem vedea în imaginea de mai sus, returnează rezultatul ca Boolean, adică ADEVĂR sau FALS.

Exemple de funcții ISEMPTY în VBA

Următoarele sunt exemplele de IsEmpty în VBA.

Exemplul nr. 1

Acum vom vedea primul exemplu practic de „ISEMPTY”. Pentru aceasta, aruncați o privire la imaginea de mai jos a foii de lucru.

Acum vom aplica funcția excel VBA ISEMPTY pentru a testa toate acestea.

Pasul 1: Definiți variabila ca booleană .

Cod:

Sub IsEmpty_Example1 () Dim K As Boolean End Sub

Pasul 2: Pentru această variabilă atribuiți valoarea prin funcția VBA ISEMPTY .

Cod:

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

Pasul 3: Expresia nu este altceva decât ce este celula pe care o testăm. Acum testăm celula A1 .

Cod:

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

Pasul 4: Afișați valoarea acestei variabile în VBA Msgbox .

Cod:

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

Rulați acest cod pentru a verifica rezultatul.

Deoarece există o valoare în celula A1 am obținut rezultatul ca FALS.

Acum voi schimba referința celulei din A1 în A5.

Cod:

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

Rulați acest cod pentru a vedea rezultatul.

Am obținut rezultatul ca fiind ADEVĂRAT celula de referință A5 este de fapt o celulă goală, așa că am obținut rezultatul ca „ADEVĂRAT”.

Acum voi testa celula A8.

Cod:

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

Rulați acest cod pentru a vedea rezultatul.

Oh!!! Stai așa…

Am obținut rezultatul ca FALS, chiar dacă nu există nicio valoare în celula A8.

Acum întrebarea este un rezultat de eroare din formula „NEMULGERE” ?.

Nu … Absolut Nu !!!

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

Avem o alternativă la funcția ISEMPTY, fără a aplica funcția excel VBA ISEMPTY putem testa efectiv celula.

Pentru un exemplu, uitați-vă la codul de mai jos.

Cod:

Sub IsEmpty_Example3 () If Range ("B2"). Value = "" Then Range ("C2"). Value = "No Update" Alt domeniu ("C2"). Value = "Actualizări colectate" End If End Sub

Linia de cod Interval („B2 ″). Valoare =” ” înseamnă dacă celula B2 este egală cu goală sau nu.

Cotațiile duble („”) reprezintă sau nu o celulă goală dacă rezultatul gol este ADEVĂRAT sau altfel FALS.

Articole interesante...