Test if a string contains only whitespaces
Robert Crandal wrote:
Does Excel VBA have a built-in function to test if a
string variable contains ONLY whitespace characters?
For my purposes, a whitespace character can be any one of
the following: space, tab, or enter (carriage return).
I know regular expressions solves this easily, but I do
NOT want to use regular expressions for this. I'm just curious
if Excel has a built-in function to test if a string contains
1 or more of the above whitespace characters.
Built in? Not that I know of, but you could try this:
Function isWhitespace(ByVal what As String) As Boolean
what = Replace(what, " ", "")
what = Replace(what, vbTab, "")
what = Replace(what, vbCr, "")
what = Replace(what, vbLf, "")
isWhitespace = CBool(Len(what))
End Function
....or this (which is the same thing, without wrapping it in a function):
onlyWhitespace = CBool(Len(Replace(Replace(Replace(Replace(what, " ", _
""), vbTab, ""), vbCr, ""), vbLf, "")))
For more whitespace characters, simply add another Replace.
--
Here's your situation, deal with it.
|