Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I use wildcards in IF statements and Macros

9 views
Skip to first unread message

AlanF

unread,
Nov 22, 2009, 5:22:01 PM11/22/09
to
I want to include in a Macro, something to take a particular action if the
active cell contains text beginning with X. I thought something based on an
IF statement, that included a wildcard, might work, e.g. IF(A1="X*,1,0). This
does not work though.

Gary''s Student

unread,
Nov 22, 2009, 5:26:01 PM11/22/09
to
Sub JustDoIt()
Dim s As String
s = ActiveCell.Value
If Left(x, 1) = "X" Then
MsgBox "We should do something"
End If
End Sub

--
Gary''s Student - gsnu200909

Dave Peterson

unread,
Nov 22, 2009, 6:08:28 PM11/22/09
to
If you meant that you wanted to include this in a formula (not a macro), then:

=if(left(a1,1)="x",1,0)
or since you're returning 1 or 0:
=--(left(a1,1)="x")

Depending on what you're doing, you may have alternatives.

If I wanted to count the number of values in A1:A10 that started with an X, I
don't need to use an intermediate helper cell with a formula.

Instead, I could use:
=countif(a1:a10,"X*")
or just to show how to separate the characters:
=countif(a1:a10,"X" & "*")
or if you point to a different cell:
=countif(a1:a10,B1 & "*")

=sumif() can also use wild cards.

--

Dave Peterson

0 new messages