how to get the first character of a string

P

Pablo Cardellino

I stored a string into a variable (the string comes from a range). The
Characters object doesn't seem to apply to an string object: just to range
and selection objects, and I am not being able to get the first character of
the string. Should I work with a range variable instead of a string one, or
there are functions for processing strings?

Thanks in advance,


Pablo Cardellino
Florianópolis, SC
Brazil
 
J

Jay Freedman

I stored a string into a variable (the string comes from a range). The
Characters object doesn't seem to apply to an string object: just to range
and selection objects, and I am not being able to get the first character of
the string. Should I work with a range variable instead of a string one, or
there are functions for processing strings?

Thanks in advance,


Pablo Cardellino
Florianópolis, SC
Brazil

Hi Pablo,

There are quite a few functions for working with strings. The one you need
immediately is the Left function, which returns a string containing the
specified number of characters from the left end of the input:

strOneChar = Left(strInput, 1)

There are also Mid and Right functions to extract other parts of strings. Also
look up the functions Split, Join, UCase, LCase, InStr, InStrRev, StrComp,
Format, StrConv, and a bunch of others that should be listed when you ask the
VBA help search for "string functions".

One other point that the purists would probably complain if I left it out:
Technically the Left, Mid, and Right functions return a Variant data type, and
the way I wrote it above would require VBA to do an implicit type conversion to
the String on the left of the equal sign. To force the function to return a
String, write it as Left$(...). In practice, I see no difference either way.
 

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