Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel Macro - Lookup-Highlight

Posted on 2016-11-23
4
Medium Priority
?
79 Views
Last Modified: 2016-11-24
Hello,
Can you please help with a Sub ,
I need to lookup the Tracking in Column "C" inside cells in column "B".
if tracking found, highlight both tracking numbers. (Red Bold Font), and in column "D" enter Line Number Found in.

Your help is appreciate.
Sample attached.
-Sample.xlsx
0
Comment
Question by:W.E.B
  • 2
  • 2
4 Comments
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 41900160
Hi,

pls try
Sub macro()
For Each c In Range(Range("C2"), Range("C" & Rows.Count).End(xlUp))
    For Each c1 In Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
        If c1 Like "*" & c.Value & "*" Then
            pos = InStr(1, c1.Value, c.Value)
            Ln = Len(c.Value)
            With c
                .Font.Bold = True
                .Font.Color = vbRed
            End With
            With c1.Characters(pos, Ln)
                .Font.Bold = True
                .Font.Color = vbRed
            End With
            Range("D" & Rows.Count).End(xlUp).Offset(1) = c1.Row
            Exit For
        End If
    Next
Next
End Sub

Open in new window

Regards
0
 

Author Comment

by:W.E.B
ID: 41900366
Hello  Rgonzo1971,
Thank you very much.
it worked.
the only thing (which is my fault, I didn't add to the sample file), if the tracking number was found on multiple lines.
is it possible to show both lines it's found on.

Also,
when I run the sub more than once, the line numbers keep showing for empty cells.

Sample attached.
Thanks again.
-Sample.xlsx
0
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 41900377
then try
Sub Macro1()
Range(Range("D2"), Range("D" & Rows.Count)).ClearContents
For Each c1 In Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
    For Each c In Range(Range("C2"), Range("C" & Rows.Count).End(xlUp))
        If c1 Like "*" & c.Value & "*" Then
            pos = InStr(1, c1.Value, c.Value)
            Ln = Len(c.Value)
            With c
                .Font.Bold = True
                .Font.Color = vbRed
            End With
            With c1.Characters(pos, Ln)
                .Font.Bold = True
                .Font.Color = vbRed
            End With
            c.Offset(, 1) = IIf(c.Offset(, 1) = "", c1.Row, c.Offset(, 1) & ", " & c1.Row)
        End If
    Next
Next
End Sub

Open in new window

0
 

Author Closing Comment

by:W.E.B
ID: 41900379
Awesome,
Thank you very much.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

886 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