Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default Test if a string contains only whitespaces

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.

Thank you.

Robert



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 538
Default 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.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default Test if a string contains only whitespaces

"Auric__" wrote in message
.116...

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



Hi Auric..... I tried the above function but it seems to return TRUE
for cases that should be false. I ran it on the below data file:

item1 scissors
item2 glue

Each line is read into a string variable. I then passed each string
variable into "isWhitespace()" one at a time and it returned TRUE
each time. Am I doing something wrong here?

Robert


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Test if a string contains only whitespaces

On Sat, 2 Feb 2013 00:40:28 -0700, "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.

Thank you.

Robert



It's a bit convoluted since the VBA string comparison operator - Like - doesn't have a quantifier. So you have to construct a string of the same length as the original, consisting of repeated character classes containing your defined whitespace characters.
One way:


=============================
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))
End Function
==============================

For long strings, I don't know that this would be any faster than using VBA's regular expression engine.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Test if a string contains only whitespaces

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


  #6   Report Post  
Posted to microsoft.public.excel.programming
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
============================
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Test if a string contains only whitespaces

How about something like this:

Function StringHasOnlyWhiteSpaces(strString As String) As Boolean

Dim i As Long
Dim arrBytes() As Byte

arrBytes = strString

For i = 0 To UBound(arrBytes) Step 2
If arrBytes(i) < 32 And _
arrBytes(i) < 13 And _
arrBytes(i) < 10 And _
arrBytes(i) < 9 Then
Exit Function
End If
Next i

StringHasOnlyWhiteSpaces = True

End Function

Note that this is on the whole a lot faster than looping through the string
with Mid,
unless the string is quite long and the non-whitespace character is at the
beginning
of the string.


RBS


"Robert Crandal" wrote in message
...
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.

Thank you.

Robert




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 538
Default Test if a string contains only whitespaces

Robert Crandal wrote:

"Auric__" wrote in message
.116...

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


Hi Auric..... I tried the above function but it seems to return TRUE
for cases that should be false. I ran it on the below data file:

item1 scissors
item2 glue

Each line is read into a string variable. I then passed each string
variable into "isWhitespace()" one at a time and it returned TRUE
each time. Am I doing something wrong here?


(This is what happens when I post while tired...)

No, Ron Rosenfeld is correct, I got the truth values reversed. The last line
should be:

isWhitespace = Not(CBool(Len(what)))

....or you can use one of the solutions Ron or RB Smissaert posted.

--
He doesn't know when to give up, this guy.
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Test if a string contains only whitespaces

Il giorno sabato 2 febbraio 2013 08:40:28 UTC+1, Robert Crandal ha scritto:

Maybe?...

Public Function HasOnlyWhiteSpaces(ByVal Text As String) As Boolean
With Application.WorksheetFunction
HasOnlyWhiteSpaces = Len(.Trim(.Clean(Text))) = 0
End With
End Function

--
Ciao!
Maurizio

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.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Test for string of space chars? Robert Crandal[_2_] Excel Programming 4 December 30th 10 08:29 PM
Test for NULL string? Robert Crandal Excel Programming 3 January 16th 10 06:59 PM
extract cusotmer name from test string Dinesh Excel Worksheet Functions 5 June 23rd 09 01:27 AM
logical test and concatenate(string) dpayne Excel Discussion (Misc queries) 7 April 5th 07 08:30 PM
Test String LucB Excel Discussion (Misc queries) 2 November 29th 06 02:49 AM


All times are GMT +1. The time now is 05:10 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"