?
Solved

How do I search column for text, and then execute something if it finds it

Posted on 2006-11-28
4
Medium Priority
?
196 Views
Last Modified: 2010-04-30
I need to search column C for the @ symbol. If it finds it, execute code A, if not, execute code B. There might be a @@ symbol in this range. How do I search for only the one @ symbol?
0
Comment
Question by:tiehaze
  • 3
4 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 2000 total points
ID: 18028310
tiehaze,

Based on your previous questions, I think you're running this from VBA and not a VB app.  If it is a vb app and you need help converting it, let me know:

Sub tiehaze()
 Dim FND As Range, FND1 As Range, FoundIt As Boolean
 Set FND = Columns("C").Find("@", LookIn:=xlValues, LookAt:=xlPart)
 If Not FND Is Nothing Then
  If FND.Value Like "*[!@]@[!@]*" Then
   FoundIt = True
  Else
   Set FND1 = FND
   Set FND = Columns("C").FindNext(FND)
   Do Until FND.Address = FND1.Address
    If FND.Value Like "*[!@]@[!@]*" Then
     FoundIt = True
     Exit Do
    End If
    Set FND = Columns("C").FindNext(FND)
   Loop
  End If
 End If
 If FoundIt Then
  Call code_A
 Else
  Call code_B
 End If
End Sub

I was guessing you were looking for an email address in there, so that the @ could not be the first or last character in the cell.  If you want it to check for the first or last as well (or only the @) then change the two lines that look like:
 If FND.Value Like "*[!@]@[!@]*" Then

To:
 If FND.Value Like "*[!@]@[!@]*" Or FND.Value Like "@[!@]*" Or _
  FND.Value Like "*[!@]@" Or FND.Value Like "@" Then

Matt
0
 
LVL 35

Expert Comment

by:mvidas
ID: 18028337
One other minor addition.. the above will call code_A if a cell contains something like "a@b@@c" and you want that excluded (since it has @@ as well as just @), then simply use
 If Not FND.Value Like "*@@*" Then

Matt
0
 

Author Comment

by:tiehaze
ID: 18028455
Awesome... that worked perfect!
0
 
LVL 35

Expert Comment

by:mvidas
ID: 18028495
Glad to help!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question