Return the first 2 letters of a Cell Value

R

RyanH

I have a BeforeDoubleClick_Event that I want to use to intialize different
Userforms. My users will click on certain cells that contain reference
numbers. Each number starts with two capital letters. The two letters will
dictate which Userform is Initalized. How can I extract the first two
letters of the cell value. For example:

Select Case (FIRST TWO LETTER OF TARGET)

Case PF
'code'
Case EC
'code'

End Select

Thanks in Advance,
Ryan
 
R

Rick Rothstein \(MVP - VB\)

Is this what you are looking for...

Select Case Left$(Target.Value, 2)

Rick
 
D

Dave Peterson

And you may want:

select case lcase(left(somecell.value,2))
case is = lcase("PF")
...
case is = lcase("EC")

You may want to do a comparison that isn't case sensitive.

I figured PF and EC were not variables--they were strings.
 
R

Rick Rothstein \(MVP - VB\)

In VB/VBA, affixing a $ sign to a String function makes that function return
a pure String value; without the $ sign, String functions return Variants
with a sub-type of String. If you need a pure String value (like for your
Case tests), the pure String functions are faster executing than the Variant
ones; however, unless you are using them in a very large loop structure, the
time difference is pretty negligiable, so using it in this case does not
really aid the execution speed in any noticeable way... I use it more from
force of habit (as I come from the compiled VB world where I encountered the
large loops much more often).

Rick
 

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