Detecting the space character

J

JN

Hi All,

I am trying to detect the space character in a string but my code is not
working.

Thanks for your help.
JN

sub demo()

Dim strlength As Integer
Dim str As String

str = "good morning"

strlength = Len(Trim(str))

For i = 1 To strlength
If Mid(str, i, strlength) = Chr(32) Then
MsgBox "space"
End If
Next i

End Sub
 
D

Dave Peterson

If Mid(str, i, strlength)
should be
If Mid(str, i, 1)

Start in position i for 1 character.
 
J

JE McGimpsey

One way:

Const str As String = "good morning"
Dim nPos As Long
nPos = InStr(str, " ")
If nPos Then Msgbox "space at character " & nPos
 
T

Tim Williams

Why not use Instr() ?

In any case, maybe you could try
....
If Mid(str, i, 1) = Chr(32) Then
....

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top