Microsoft Excel - Macro to search a cell and not match entire cell contents

Asked By Sharnell I
21-Oct-10 07:18 PM
Hi, I currently have the following macro, but it errors out:
Sub test()
Dim rng As Range, c As Range, cfind As Range, rng1 As Range
Worksheets("mail_list2.4_2.5").Activate
Set rng = Range(Range("H2"), Range("H2").End(xlDown))
Set rng1 = Range(Range("L2"), Range("L2").End(xlDown))

For Each c In rng
Set cfind = rng1.Cells.Find(What:=c.Value, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False)
If Not cfind Is Nothing Then c.Interior.ColorIndex = 3
Next c
End Sub

I am trying to compare two columns of email addresses, column L has my range of address, and column H has the address I need to highlight, column H has cells that have multiple email address, so essentially, I need the macro to not match entire cell contents, just look within that cell for a possible matching email address within L and highlight the cell(in H).  See below:
Column L:
anthony.b@domain.com April.T@domain.com arthur.G@domain.com Babette.A@domain.com Barbara.E@domain.com barbara.M@domain.com barton.B@domain.com

Column H:
marjorie.p@domain.com;tony.p@domain.com;barton.b@domain.com carol.m@domain.com; charisma.s@domain.com;babette.a@domain.com nikisha.c@domain.com; david.f@domain.com;nancy.m@domain.com; chris.l@domain.com; chris.l@domain.com;

Thanks in advance for any assistance, I've been struggling with this for quite a while.... =(
  Rolf Jaeger replied to Sharnell I
21-Oct-10 09:19 PM
Hi Sharnell:

what do you mean by 'it errors out'?

I tested your code and if I correctly understand what you are trying to do you were close. It seems that you would just need to replace this statement:

If Not cfind Is Nothing Then c.Interior.ColorIndex = 3

with this:

If Not cfind Is Nothing Then cfind.Interior.ColorIndex = 3

Hope this helped,
Rolf
  Sharnell I replied to Rolf Jaeger
21-Oct-10 09:37 PM
Hi, Thank you so much for replying, I get a "Run-time error '13':
Type mismatch for this statement:
Set cfind = rng1.Cells.Find(What:=c.Value, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False)
  Sharnell I replied to Rolf Jaeger
21-Oct-10 11:13 PM
Hi Rolf,

I opened the sheet in another Excel and was able to get the Macro to work.  Thanks again for your input!!!!! Please mark this as happily resolved!

=)
Sharnell
  Rolf Jaeger replied to Sharnell I
22-Oct-10 12:09 AM
Hi Sharnell:

I didn't get that error with the data you posted, but generally it is a good idea to include error trapping. Give this code a try:

On Error Resume Next
For Each c In rng
  Set cfind = rng1.Cells.Find(What:=c.Value, LookAt:=xlPart, _
  SearchOrder:=xlByRows, MatchCase:=False)
  If Not cfind Is Nothing Then cfind.Interior.ColorIndex = 3
  cfind = Nothing
Next c

Hope this will help,
Rolf
Create New Account
help
How can I make my data show as flashing in Excel? Excel Excel Miscellaneous Discussions Microsoft Excel (1) Excel (1) Application.OnTime (1) Worksheet (1) Workbook (1) Macro (1) VBA (1) Font.ColorIndex (1 if you insist, see Chip Pearson's site for VBA code. http: / / www.cpearson.com / excel / BlinkingText.aspx Gord Dibben MS Excel MVP This was great - thank you. My boss loved it. Now I have another question
Printing Excel Worksheets Excel Is there a possible way to print a double sided worksheet from two separate worksheets? Excel Miscellaneous Discussions Microsoft Excel (1) Worksheets (1) Excel (1) I assume you mean double-sided sheet of paper from two worksheets
VBA to compress photos in an excel worksheets Excel I need to write some VBA code, to compress several photos in my excel worksheets. Is there anyone can help me? Thanks! Excel Miscellaneous Discussions VBA (1) Afb1389213c28977 (1) Gsnu200798 (1) Lnk (1) Gst (1) http: / / groups.google com / group / microsoft.public.excel.programming / browse_thread / thread / afb1389213c28977 / 1201ee9ee3bd3c87?lnk = gst&q = compress+pictures#1201ee9ee3bd3c87 - - Gary''s Student - gsnu200798
How to find exact differences between two worksheets Excel Excel Worksheet Discussions Microsoft Excel (1) Uniques (1) AEC04CCD8AD6 (1) Bliengme (1) Caps (1) Duplicates This site is great for finding Uniques / Duplicates: http: / / www.cpearson.com / excel / Duplicates.aspx PLEASE write your message in this white space next time best wishes - - Bernard V Liengme Microsoft Excel MVP http: / / people.stfx.ca / bliengme remove caps from email keywords: How, to, find, , exact
Is there any option to group Worksheets? Excel Please, let me know if there is any option in Microsoft Excel to Group Worksheets. Excel Miscellaneous Discussions Microsoft Excel (1) Worksheets (1) Hiright (1) Ungroup (1) Chick (1) hi right click any sheet tab