How do you say "begins with"

  • Thread starter Thread starter michael_mcclellan
  • Start date Start date
M

michael_mcclellan

Hi there,

I am writing a function that needs to have a statement in it that goes

If [data in ActiveCell] begins with "P"
then do something.

How do I do this?
 
Hi Michael,
I am writing a function that needs to have a statement in it that goes

If [data in ActiveCell] begins with "P"
then do something.

How do I do this?

Are you writing a VBA user-defined function (UDF), or are you looking for a
worksheet function solution?

If you want to use worksheet functions:

=IF(LEFT(A1,1)="p", "yes", "no")

If VBA:

If UCase$(Left$(ActiveCell.Value, 1)) = "P" Then
MsgBox "yes"
Else
MsgBox "no"
End If

The worksheet example isn't case sensitive. If you want only capital P's to
be matched, remove the UCase$() from the second example.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
Perhaps something like this?

Sub StartingwithP()
If Left(Selection.Value, 1) = "p" Then
MsgBox "woohoo"
End If
End Sub

tj
 
Hi there,

I am writing a function that needs to have a statement in it that goes

If [data in ActiveCell] begins with "P"
then do something.

How do I do this?

If Left(ActiveCell.Value,1) = "P" Then
Do Something
End If

-gk-
 
Mine should be ActiveCell.Value, like the other answers posted.

tj

tjtjjtjt said:
Perhaps something like this?

Sub StartingwithP()
If Left(Selection.Value, 1) = "p" Then
MsgBox "woohoo"
End If
End Sub

tj

Hi there,

I am writing a function that needs to have a statement in it that goes

If [data in ActiveCell] begins with "P"
then do something.

How do I do this?
 
Alternative, purely for the sake of diversity ...

If ActiveCell.Value Like "[Aa]*" Then
Do Something
End If

Oh, and I like the "Like" operator ;o)


| If Left(ActiveCell.Value,1) = "P" Then
| Do Something
| End If
 
Alternative, purely for the sake of diversity ...

If ActiveCell.Value Like "[Aa]*" Then
Do Something
End If

Oh, and I like the "Like" operator ;o)


| If Left(ActiveCell.Value,1) = "P" Then
| Do Something
| End If
 

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

Back
Top