Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXCEL VBA Check if entry is empty or not 'space'

Tags:

excel

vba

space

Note. Check if the TextBox1 is empty is easy by using TextBox1.Value = "".

But the problem is when the user hit the spacebar, TextBox1 will still recognize it as a value. In such case, my data will appear as an empty cell with 1 space inside. So my question is, is there any method to check TextBox1.value for empty and also not consist of space whether there are 1 or more space? Million thanks to all.

like image 783
4 Leave Cover Avatar asked Jan 01 '13 08:01

4 Leave Cover


People also ask

How do you check if cell value is empty in Excel VBA?

If you wish to test whether a worksheet cell is empty in VBA, you can not use the worksheet function called ISBLANK. In VBA, you must use the ISEMPTY function. In this example, we will test whether cell A1 is empty. If cell A1 is empty, the message "Cell A1 is empty" will be displayed.

Is empty or blank VBA?

Excel's IsBlank is very restrictive and will only ever give True if the cell is completely empty (not even has a formula that returns empty result). So if that returns True , IsEmpty on the cell's value should also return True. But what IsEmpty(cell) actually does is detecting whether the cell 's .

Is null VBA Excel?

ISNULL is a built-in function in VBA and is categorized as an Information function in VBA which returns the result in Boolean type i.e. either TRUE or FALSE. If the testing value is “NULL” then it returns TRUE or else it will return FALSE.

What is empty string in VBA?

'Empty indicates a Variant variable for which you do not explicity specify an initial value, which by default gets initialized in VBA to a value that is represented as both a zero and a zero-length string.


2 Answers

A common trick is to check like this:

trim(TextBox1.Value & vbnullstring) = vbnullstring 

this will work for spaces, empty strings, and genuine null values

like image 84
Lord Peter Avatar answered Sep 28 '22 19:09

Lord Peter


Most terse version I can think of

Len(Trim(TextBox1.Value)) = 0 

If you need to do this multiple times, wrap it in a function

Public Function HasContent(text_box as Object) as Boolean     HasContent = (Len(Trim(text_box.Value)) > 0) End Function 

Usage

If HasContent(TextBox1) Then     ' ... 
like image 24
pyrospade Avatar answered Sep 28 '22 21:09

pyrospade