highlight the matching row - vlookup vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I have a table with 2 columns, Name and Phone# column. I wrote vlookup code (see below) and it is working fine. I want to modified it so that excel will highlight the row that contains that name and phone number. Is that possible? Thank you so much.
++

Sub price()
Dim item As String
Dim x As String
item = InputBox("enter Name")
Sheets("price").Activate
x = WorksheetFunction.VLookup(item, Range("A2:B171"), 2, False)
MsgBox item & " phone# is --> " & x


End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try:
Code:
Sub price()
    Dim item As String
    Dim rng As Range
    With Sheets("price")
        .Range("A2:B" & .Range("B" & .Rows.Count).End(xlUp).Row).Interior.ColorIndex = xlNone
    End With
    item = InputBox("Enter Name")
    Set rng = Sheets("price").Range("A:A").Find(item, LookIn:=xlValues, lookat:=xlWhole)
    If Not rng Is Nothing Then
        rng.EntireRow.Interior.ColorIndex = 3
    Else
        MsgBox ("Name not found.")
    End If
End Sub
 
Last edited:
Upvote 0
Thank you so much, it works amazing. It did highlight all the row. Sorry for bothering you, is it possible to highlight only the 2 cells in that row, the one has the Name and the Phone number? Thank you once again for this great help.
 
Upvote 0
Try:
Code:
Sub price()
    Dim item As String
    Dim rng As Range
    With Sheets("price")
        .Range("A2:B" & .Range("B" & .Rows.Count).End(xlUp).Row).Interior.ColorIndex = xlNone
    End With
    item = InputBox("Enter Name")
    Set rng = Sheets("price").Range("A:A").Find(item, LookIn:=xlValues, lookat:=xlWhole)
    If Not rng Is Nothing Then
        rng.Resize(, 2).Interior.ColorIndex = 3
    Else
        MsgBox ("Name not found.")
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top