View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld[_2_] Ron Rosenfeld[_2_] is offline
external usenet poster
 
Posts: 1,045
Default Test if a string contains only whitespaces

On Sat, 02 Feb 2013 08:01:20 -0500, Ron Rosenfeld wrote:

On Sat, 2 Feb 2013 11:12:55 +0000 (UTC), "Auric__" wrote:

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


For VBA, I think you have that reversed.
False = 0


Maybe "reversed" is not the rigtht word.
You might use : isWhitespace = CBool(Len(what)-1)) but that would return True for a null string also; so you'd need to check that the original "what" is not a null string, if I understand the OP's requirements.
And, unexpectedly, my UDF also returns True if s = "".

To correct for that in mine, try:
=============================
Option Explicit
Function WhiteSpacesOnly(s As String) As Boolean
'White space character class
Const wSpCC As String = "[ " & vbLf & vbTab & vbCr & "]"
WhiteSpacesOnly = (s Like WorksheetFunction.Rept(wSpCC, Len(s))) * Len(s)
End Function
============================