Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA test to see if string is empty. 1

Status
Not open for further replies.

dprayner

Programmer
Oct 14, 2002
140
US
Hi people what is the best way to test a string to see if it is empty? I was thinking maybe using If Not IsEmpty(string) Then. Thank you, DAVE
 

Hi,

String variables cannot be Empty. Empty has a specific meaning that does not apply to string variables.

Either the length of a string is zero or the variable is equal to "" as ...
Code:
If MyVar = "" Then...
See VB Help on Empty, Null as words that are often misunderstood.

Skip,

[glasses] [red]Be Advised![/red] The Vinyards of Texas have produced a wine with diuretic dimishment and urethric relief...
Pinot More![tongue]
 
I have read that using
Code:
If Len(MyStr) = 0
is faster than
Code:
If MyStr = ""
which is only important if you are testing many strings, say inside a loop.

Regards,
Mike
 
Darn, my computer is too fast. I did a test of 100 strings and could not get a difference.

Gerry
 
Using GetTickCount API. Is there a faster alternative?

Gerry
 
In my testing, If strTest = "" took 7% less time than If Len(strTest) = 0

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
GetTickCount is actually only accurate to about 10 - 15ms on XP/nt/2000/2003 (e.g. its 15.625ms on my PC)

Faster alternatives:
1ms: multimedia timers, eg timeGetTime (although you may need to play around timeBeginPeriod and timeEndPeriod to ensure this, depending on your OS)
Better than 1ms: QueryPerformanceCounter/QueryPerformanceFrequency - high performance timers. Resolution is dependant on your hardware - on my AMD2800 I get a resolution of approx 280ns (yes, that's nanoseconds)

It is probably worth pointing out that at modern PC speeds even an accuracy of 1ms wouldn't be enough to measure the difference on 100 iterations. But using the high performance timers, we can just start seeing that rmikesmith's assertion is correct (as to whether the speed difference is important enough to worry about in a real world application ... I'd say not)

thread222-1089225 and thread222-495997 might be of interest. There are several additional links in those threads
 

The original question was what is the best way to check for an empty string, not what is the fastest way.

I believe - and my own tests of millions of iterations have borne this out - that Mike is correct to say that checking for a length of zero is faster than checking explicitly against an empty string.

I would, however, suggest that checking against an empty string is clearer in most circumstances and would say that was probably the best.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Oh I certainly was not disputing that Len = 0 is faster. I did get a difference but it seemed so small that I was wondering if it was within some sort of margin of error.

I also agree with Tony in that there may be something to say for the clarity of checking the contents, rather than the size of the container. Especially as the speed issue is (generally) not really much of a concern.

However, good links. Thanks!

Gerry
 
Oh, and I thought that was worth a star.

Gerry
 
Gents,

Didn't intend to steer this thread into an esoteric technical discussion, but the side trips are often quite interesting. Strongm, thanks for the very good info on timers. I agree with Tony that the strVar = "" test is probably the best in all but the most extreme situations. It is the form I almost always use.

BTW, my source for the speed comparison is the excellent reference VBA Developer's Handbook by Ken Getz & Mike Gilbert.

Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top