vbNullString

Digitborn.com

Active Member
Joined
Apr 3, 2007
Messages
353
Hello,

What's the difference when you use vbNullString or "". For example:

TextBox1.Text = " "
TextBox1.Text = "vbNullString"

Are you saving memory or anything else?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi
Yes - to your Minor Point.
Thanks sijpie.

_ ...................
P.s. A couple of Threads running in parallel have some interesting discussions along the lines of variable declarations “referring to pointers“ or Pigeon holes with an ( initial) or start Address etc...
http://www.mrexcel.com/forum/excel-questions/917689-passing-array-class-byval-byref.html
http://www.mrexcel.com/forum/excel-questions/917675-passing-objects-byref-byval.html

_.. Interesting one learns initially the idea of a declaration of a variable as setting aside the appropriate space in computer memory. Then as we get into more detail that only appears, if at all, to be for things like numbers.
 
Upvote 0
Some people say that
Code:
If Len(sStr) = 0 Then
is the fastest way. With a simple time test loop I found that for VBA this is not the case. I think that this is because as I mention above, testing for vbNullString only involves checking if the address is the same as vbNullstring address, whereas testing the length involves more steps

As a matter of interest, what was your test code? Unless you know that your variable was never assigned a value, or was cleared explicitly using vbNullString, I'd be surprised if you see greater efficiency using vbNullString since the address will not be the same - i.e. StrPtr won't return 0 for your variable.

As best I recall the last time I looked at it, you're talking hundredths of a second to do 1m comparisons whichever way you choose, so I'll probably save myself some typing. ;)

I also don't believe that explicitly setting objects to nothing will resolve any memory leaks you might experience - that doesn't actually trigger garbage collection directly.
 
Upvote 0
@Rory and sijpie
As a matter of interest, what was your test code? .....
In all these considerations i am still not usually seeing any consideration of “....what about Empty......“ ( from about posts # 15 ) and using that instead for the comparison. I mean my basic ideas ( for my stuff ) was to “get rid of stuff” in an Array that i did now want using
= Empty
Then correspondingly i do a
If ______ = Empty

An alternative would be to first do the
= Empty,
Then
= vbNullString
And then do a
If ______ = vbNullString ____ ( or If Len(_____) = 0 _____

... but maybe i see the point it may all be splitting hairs a bit, but if anyone did happen to do any speed tests, i would be interested if an additional
= Empty
could be done.

Note the point my experiments showed that you can do a
= Empty
check against Strings, Longs and even Objects etc, and for the corresponding..... “not here”, “zero length”, “not being anything”, “Not having an address of where something actually is “ , ( or “WTF existence is Not being !?” ) ..... a True is returned... convenient for checking things that may be of different types.. maybe..

Alan
 
Last edited:
Upvote 0
Empty is a Variant subtype. If you use = Empty, the compiler will have to create a variable of the relevant type and then perform the comparison. (the appropriate test for a variant is IsEmpty)

Also, FYI, vbNull is a constant with value 1 - it is not any kind of null/nullable data type.
 
Upvote 0
Hi Rory,
Thanks for the reply..
...vbNull is a constant with value 1 - it is not any kind of null/nullable....
Thanks for clearing that up. It was looking as though that was what it was.. but I could not google anything to clarify that. So I can ignore it in consideration of Null / Nothing Empty stuff. Saves me looking further.
_....

Empty is a Variant subtype. If you use = Empty, the compiler will have to create a variable of the relevant type and then perform the comparison. (the appropriate test for a variant is IsEmpty)...
I see now I was doing something a bit stupid. As you said, = Empty was making a relevant type for the comparison. So I am fooling myself a bit using it as a check for the relevant type of “Null”, “Not there”, “or “what the Not nothing Not there where wotever” .
And I just saw that doing the
If IsEmpty(________) ____
On a Variable type other than a Variant which is vbNullString, = “”, 0 or whatever, does not indeed give me True

But if speed was not too important it could still be a possibility, for the case of my Variant Variables.

But the better thing to do would be ( for the Variant Variable case ) to empty them with
= Empty,
Then do any comparison with
If IsEmpty(____)

So there still may be some worth in doing some comparison speed tests, but probably again splitting hairs a bit.

The comparison i guess would then be what is quicker. ( For the variant Variable case )

_A)
Put
= Empty
Do
If IsEmpty(____)

_B)
Put
= Empty
Do
= vbNullString ( or one of the other Null”, “Not there”, “or “what the Not nothing Not there wotever” )

Do
If _______ = vbNullString ( or the appropriate other “of the other Null”, “Not there”, “or “what the Not nothing Not there wotever” )



Thanks
Alan


P.s. BTW Do you ( or anyone ) happen to know the answer to that?. I guess A) should be quicker than B).
I will have a go sometime anyway when I get the time.
 
Last edited:
Upvote 0
B would require you to know what was inside the Variant at design time.
 
Upvote 0
Hi
B would require you to know what was inside the Variant at design time.
I did not think "trashing" stuff would make any difference as to what was in the Element of variant type in an Array ( Data Field of variant types ) . I guess it works like deleting files then , takes a bit longer if they are big?
In my case there would ( for now ) most likely just be strings of text, not more than about 100 characters in some Elements and i do not want them long text strings ( as i will paste the whole Array out again in one go ), and the Variant Elements which I did not want to “empty” would have shorter text like “100 mg” or similar. What i do is copy a big list from something like a web site to the clipboard, paste it in a spare sheet, then apply a code which captures the values in one go from the pasted in selection. It then loops through the Array, picks out what i want and empties the Elements with stuff in that I do not want. ( I then further strip everything I do want down to a simple number with the same units and so need no unit qualifier – keeps the data nice and simple! So here 100 mg becomes O.1 ) )


At the end of the day, time may not be really a big issue. I just thought before i start doing it big time i would check out the best way to do it. And also i wanted to get clear in me head the different “ not having stuff in etc.. “ things. Clearly i had some dopey ideas about Empty.
But i think i have almost got that one now.

I just had the gut feeling that using Variant Elements ( which the “one liner capture forces me to use ( declare in my array) anyway due to the returned field of Variant Elements ( for more than one cell ) ) and “emptying” was a good idea. Otherwise a __“”__ or a __0__ slipping in when i re paste in often chucks a type mismatch error “Spanner in the Works” at some further point. Pasting Empty Variant Elements of an Array into a sheet where i do not want anything done ( and the format not being effected ) seems fairly well behaved. Pasting in a __0 __or __“” __or__ “wot not here nothing WTF” __ as i mentioned can put a "Spanner in the works"..

Alan

---o00o---`(_)`---o00o---
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top