EXCEL VBA Check if entry is empty or not 'space'
Asked Answered
V

4

41

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.

Vasoinhibitor answered 1/1, 2013 at 8:15 Comment(0)
S
87

A common trick is to check like this:

trim(TextBox1.Value & vbnullstring) = vbnullstring

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

Sulphate answered 1/1, 2013 at 8:18 Comment(4)
Dear Sir, this works perfectly! But is there any shorter version of it? Do I have to type or copy/paste for every TextBox?Vasoinhibitor
You could use "" instead of vbNullString. (vbNullString is just an internal pointer to a common instance of ""). But I wouldn't worry about the length of the expression - it is stored in tokens, so the length doesn't matter. To save typing you can copy/paste the expression using Ctrl-C/Ctrl-V and change it as needed.Sulphate
It is working well. I will be using Trim(TextBox1.Value & "") = "" from now onwards. Once again thank you :)Vasoinhibitor
Using vbNullString is actually a better option since it requires less byte size to execute.Kilah
H
32

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
    ' ...
Homerus answered 1/1, 2013 at 8:38 Comment(4)
Dear Sir, this is brilliant too. I would definitely use this in my project. I'm checking for a lot TextBox and this would be time saving. Thank you very much!Vasoinhibitor
It gave me an error Type mismatch. Any idea how would that happen?Vasoinhibitor
Probably because of the TextBox class. I changed it to object here, so that should correct it!Homerus
Changed the comparison from = to > to better reflect the meaning of HasContentsHomerus
P
4

Here is the code to check whether value is present or not.

If Trim(textbox1.text) <> "" Then
     'Your code goes here
Else
     'Nothing
End If

I think this will help.

Pavia answered 1/1, 2013 at 8:21 Comment(6)
Your code is not working Sir. It's not checking for spacebar. By the way, I've marked above answer and I truly appreciate your faster reply too. Once again thank you!Vasoinhibitor
This code will remove starting and ending space from value in textbox. The code must work, may I know what is the error you are getting please.Pavia
Sorry for my misunderstanding. I meant is that I want to check whether the user hit the spacebar in the TextBox1. Your code do not check for single and multiple space. What I want is to only check for space without any other alphanumeric.Vasoinhibitor
Then also this code will work. for example if I have entered just a space in textbox and nothing else then this code will consider that textbox contains null. If you want to consider space then you can yes keyascii function.Pavia
I've revised your code, I think there should be = instead of <> in Trim(textbox1.text) <> "".Vasoinhibitor
In this case if there is no space in textbox then also code will return true.Pavia
M
-3

You can use the following code to check if a textbox object is null/empty

'Checks if the box is null

If Me.TextBox & "" <> "" Then

        'Enter Code here...

End if
Mesnalty answered 24/4, 2016 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.