VBA Vlookup with conditional formatting

Lisa05

New Member
Joined
Nov 25, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I have a vba macro which vlookups cells in the same workbook between two tabs and replaces values in the origin cell to the value found. Is it possible to introduce conditional formatting that would indicate where vlookup actually replaced the cell value and which values were actually the same? Is it possible to combine conditional formatting with vlookup, like let's say first format cells that differ and then use vlookup to replace the value? Thank you in advance for help.

This is my code for vlookup:

Sub vlookup ()

Dim dataws As Worksheet
Dim searchdataws As Worksheet
Dim datalr As Long
Dim searchdatalr As Long
Dim datarange As Range
Dim x as Long

Set searchdataws = ThisWorkBook.Worksheets("Search")
Set dataws = ThisWorkBook.Worksheets("Data")

datalr = dataws.Range("B" & Rows.Count).End(xlUp).Row
searchdatalr = searchdataws.Range("B" & Rows.Count).End(xlUp).Row

Set datarange = searchdataws.Range("B2:D" & searchdatalr)

For x = 2 To datalr
On Error Resume Next
dataws.Range("D" & x).Value = Application.WorksheetFunction.VLookup(dataws.Range("B" & x).Value, datarange,3,0)

Next x

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi & welcome to MrExcel.
Roughly how many rows of data do you have on each sheet?
 
Upvote 0
Ok, how about
VBA Code:
Sub Lisa()
   Dim Cl As Range
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Search")
      For Each Cl In .Range("B2", .Range("B" & Rows.Count).End(xlUp))
         Dic(Cl.Value) = Cl.Offset(, 2).Value
      Next Cl
   End With
   With Sheets("Data")
      For Each Cl In .Range("B2", .Range("B" & Rows.Count).End(xlUp))
         If Dic.exists(Cl.Value) Then
            If Dic(Cl.Value) <> Cl.Offset(, 2).Value Then
               Cl.Offset(, 2).Value = Dic(Cl.Value)
               Cl.Offset(, 2).Interior.Color = rgbCadetBlue
            Else
               Cl.Offset(, 2).Interior.Color = xlNone
            End If
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

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