How to find any of a set of characters in a string

P

Philip Brown

Hello,

How do I determine if one or more of a set of characters occurs in a given
string?

For example, I need to determine if d, D, or r occurs in the current .range
object.

I was hoping for something like:

InStr(1, .range, "[aDr]") but that doesn't work.

Any help would be appreciated!
 
P

Philip Brown

I solved my problem using the following

If .moveuntil(CSet:="dDr", Count:=5) = 0 Then ...

The range object is not changed if none of the characters specified in CSet
are found. MoveUntil returns a zero is the range object is not changed. In
this way, I have verified that none of the specified characters exists in the
range.
 
H

Helmut Weber

Hi Philip,

like this:

Sub Test900()
MsgBox IsInRange(selection.Range, "dDr")
End Sub
' -------------------------------
Public Function IsInRange _
(rngTmp As Range, strTmp As String) As Boolean
IsInRange = False
With rngTmp.Find
.Text = "[" & strTmp & "]"
.MatchWildcards = True
If .Execute Then
IsInRange = True
End If
End With
End Function


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
M

muyBN

If you have some text selected, then the following code will test if the
selection is any of the following: lower case a through z; upper case a
through z; or digits 0 through 9.

If Selection Like "[a-z/A-Z/0-9]" Then...

Using this as a model, experiment replacing what I have between the
brackets. I believe the name for it in VB is "normal expression" if you want
to do more research.

I work with Selection more than I do with Range, but I believe that if you
want to test in a certain range, you would write .Range then .whatever after
that.

What I've written is valid but maybe someone else with more exerience in
these other areas can fill in the blanks.
 

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