Using Excel VBA - Finding Characters in Strings

  • Thread starter Thread starter Noitide
  • Start date Start date
N

Noitide

Hello,

I am trying to figure out whether or not a string has a space in
its contents. I am using the following code.
(AppWSF is set equal to Application.WorksheetFunction)

If IsError(AppWSF.Find(" ", Given_Data.Value, 1)) = False Then
HasSpace = True
SpaceLoc = AppWSF.Find(" ", Given_Data.Value, 1)
End If

It works fine if there is a space in the string; however, it has an
error and quits running if there is no space in the string. Any help
would be appreciated!

Thanks,
 
Hello,

     I am trying to figure out whether or not a string has a space in
its contents. I am using the following code.
(AppWSF is set equal to Application.WorksheetFunction)

If IsError(AppWSF.Find(" ", Given_Data.Value, 1)) = False Then
        HasSpace = True
        SpaceLoc = AppWSF.Find(" ", Given_Data.Value, 1)
End If

It works fine if there is a space in the string; however, it has an
error and quits running if there is no space in the string. Any help
would be appreciated!

Thanks,

without trying it myself
Replaced=replace(Given_data.value," ","")
if Replaced=Given_data.value then
HasSpace=true
else
HasSpace=false
endif
greetings
bart
 
without trying it myself
Replaced=replace(Given_data.value," ","")
if Replaced=Given_data.value then
   HasSpace=true
else
  HasSpace=false
endif
greetings
bart- Hide quoted text -

- Show quoted text -

sorry something wrong
if Replaced=Given_data.value then
must be
if Replaced<>Given_data.value then

bart
 
I am trying to figure out whether or not
a string has a space in its contents.

Would this work?

Function HasSpaceQ(S As String) As Boolean
HasSpaceQ = S Like "* *"
End Function
 
You could use VBA's equivalent of =find().

if instr(1, given_data.value, " ", vbtextcompare) > 0 then
'has a space
else
'doesn't have a space
end if
 
Back
Top