Solved

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

Posted on 2006-11-28
4
168 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Awesome... that worked perfect!
0
 
LVL 35

Expert Comment

by:mvidas
Comment Utility
Glad to help!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now