Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Check if variable is empty

I have an object and within it I wanna check if some properties is set to false, like:

If (not objresult.EOF) Then   'Some code  End if 

But somehow, sometimes objresult.EOF is Empty, and how can I check it?

  • IsEmpty function is for excel cells only
  • objresult.EOF Is Nothing - return Empty
  • objresult.EOF <> null - return Empty as well!
like image 493
fessguid Avatar asked Jul 25 '10 12:07

fessguid


People also ask

How do you check if a variable is empty in Excel VBA?

The ISEMPTY function returns TRUE if the value is a blank cell or uninitialized variable. The ISEMPTY function returns FALSE if the value is a cell or variable that contains a value (ie: is not empty).

Is empty or null VBA?

The Null value indicates that the Variant contains no valid data. Null is not the same as Empty, which indicates that a variable has not yet been initialized. It's also not the same as a zero-length string (""), which is sometimes referred to as a null string.

Is empty in VBA?

VBA IsEmpty is a logical function that tests whether selected is empty or not. Since it is a logical function it will return the results in Boolean values i.e. either TRUE or FALSE. If the selected cell is empty it will return TRUE or else it will return FALSE.

How do you check if a variable contains a string in VBA?

The VBA InStr function is one of the most useful string manipulation functions around. You can use it to test if a string, cell or range contains the substring you want to find. If it does, “InStr” will return the position in the text string where your substring begins.


1 Answers

How you test depends on the Property's DataType:

 | Type                                 | Test                            | Test2 | Numeric (Long, Integer, Double etc.) | If obj.Property = 0 Then        |  | Boolen (True/False)                  | If Not obj.Property Then        | If obj.Property = False Then | Object                               | If obj.Property Is Nothing Then | | String                               | If obj.Property = "" Then       | If LenB(obj.Property) = 0 Then | Variant                              | If obj.Property = Empty Then    | 

You can tell the DataType by pressing F2 to launch the Object Browser and looking up the Object. Another way would be to just use the TypeName function:MsgBox TypeName(obj.Property)

like image 55
Oorang Avatar answered Sep 23 '22 02:09

Oorang