Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

higlight new data across sheets with matching in another

0

hello 

I  try  making  code  to  highlight   new  data  in sheets rep1,proc1,  but they  are   not  existed  in sheet  RP  as  I  highlighted   in sheets rep1,proc1  should  be. 

it  should  match  column B,C,D together    in sheet RP  with sheets rep1,proc1  into column B  .if  the  items in column B for sheets rep1,proc1  are  not  matched with sheet RP into  column B,C,D  then  should  highlighted  by  red .

I  truly  appreciate  if  any body  guide  me  the  right  way  to  do  that .

Sub hig()
Dim ws As Worksheet
Dim ws1  As Variant
Dim i, s, lr As Integer
Dim mFind As Range

Set ws = Sheets("RP")
 ws1 = Array("rep1", "proc1")
lr = ws.Range("b" & Rows.Count).End(xlUp).Row
With ws
For Each Cell In Range("b2:D" & lr)
With Sheets(ws1)
        Set mFind = .Columns("B").Find(Cell.Value)

   If .Range("B" & mFind.Row).Value <> (Cell.Value) Then
                            .Range("B" & mFind.Row).Interior = vbRed
                            End If
                            End With
                            Next Cell
                            End With

End Sub

Answer
Discuss

Answers

0
Selected Answer

Hasson 

Here's a method of doing what you want using a VBA dictionary. That's a bit like an index in a book, arranged alphabettically with two parts- a key and a value. In this case I've combined three cells from sheets RP to form the keys and saved a cell address as the dictionary value (but it could be the count of instances, or location- worksheet name/cell say- like for a word/ page number in a book index).

I've done that since dictionaries have the very useful method .exists(value) to see very quickly if a record like that exists (and where the value can be your "unsplit" values in sheets RP and proc1).

That means there are two main steps to the new code below (in new module 2 and under the code comment lines):

'### 1.  create a dictionary combining cells as a key) and column B address (as dictionary value)

'### 2.  loop through column B of sheets and see if value exists in dictionary

I've added a new button called "Highlight mismatches" to sheet RP and you can click that to run the (commented) code below manually:

Option Base 1
Sub HighByDict()
Dim ws As Worksheet, lRow As Long
Dim i As Long
Dim dict As Object, Cll As Range
Dim s As String, valArray As Variant, n As Long

Set dict = CreateObject("Scripting.Dictionary")
Set ws = Sheets("RP")
ws.Activate
lRow = Range("B" & Rows.Count).End(xlUp).Row

'### 1.  create a dictionary combining cells 9as a key) and column B address (as dictionary value)
For i = 2 To lRow
    If Len(Cells(i, 2)) > 0 Then
        'convert range to an array
        valArray = Application.Transpose(Application.Transpose(Cells(i, 2).Resize(1, 3).Value))
        'create dictionary key by joining array using space as delimiter
        s = Join(valArray, " ")
        'check if dictionary key exists and if not create a new key with value as address in B
        If Not dict.exists(s) Then dict(s) = Cells(i, 2).Address
    End If
Next i

'### 2.  loop through column B of sheets and see if value exists in dictionary
rr = Array("rep1", "proc1")
' loop from first element of rr to last
For i = 1 To UBound(rr)
    ' work with that worksheet
    With Sheets(rr(i))
        'clear any existing colour fills
        .UsedRange.Offset(1, 0).Interior.Color = xlNone
        ' get last row
        lRow = .Range("B" & .Rows.Count).End(xlUp).Row
        'loop down B to last row
        For Each Cll In .Range("B2:B" & lRow)
            ' check if the value is a dictionary key
            If dict.exists(Cll.Value) Then
                ' get cell using address from dictionary value
                With ws.Range(dict(Cll.Value))
                ' ### split value into parts from RP
                    'cell.Resize(1, 3).Value = .Resize(1, 3).Value
                End With
                
                Else
                'if not in dict then make 3 cells red
                Cll.Resize(1, 3).Interior.Color = vbRed
                n = n + 1
            End If
        Next Cll
    End With
Next i

' advise user
MsgBox "Found " & n & " value(s) without an exact match in sheet " & ws.Name

End Sub

Note that you will get 5 (not two) areas in red and below I've given you the reasons as comments:

Sheet RP:

QQW-712  S** CLA7 US ' does not exist in RP



Sheet proc1:

QQW-15 CLA5 EG  ' does not exist in RP (but ...TU does)

QQW-13 CLA11 TR ' says TU not TR in sheet RP

QQW-10 BN CLA10 IT -MM  ' says IT (without -MM) in sheet RP

QQW-15 L/R CLA14 SS230 EG ' says QQW-15 (without L/R) in sheet RP

In your previous files, you have split the combined values into columns. If you want to do that at the same time here, within the new code just uncomment the second line below (as follows):

' ### split value into parts from RP

cell.Resize(1, 3).Value = .Resize(1, 3).Value

However in a Discussion point below (not the original Question) you asked for the sheets to be checked "dynamically" if sheet RP is changed. I've done that by adding extra lines to the macro above (within a With/End With) to clear the colour fills per sheet:

        'clear any existing colour fills

        .UsedRange.Offset(1, 0).Interior.Color = xlNone

and by adding a Worksheet_Change event macro which only checks if there are values in columns B, C and D of a row. That code is behind sheet RP and is shown below, commented so you can see what's happening:

Private Sub Worksheet_Change(ByVal Target As Range)
' do nothing if a cell outside columns B:D was changed
If Intersect(Target, Columns("B:D")) Is Nothing Then Exit Sub

With Cells(Target.Row, 2)
    ' If there are entries in B, C and D...
    If .Value <> "" And .Offset(0, 1).Value <> "" And .Offset(0, 2).Value <> "" Then
        ' ... recheck sheets RP and proc1
        Call HighByDict
    End If
End With
End Sub

(It will give the Message Box from macro HighDict (and you might want to check that it reports 0 mismatches before you do other things with the file- like splitting the cells in RP and proc1)

Finally, I've corrected a typo in your headings (again) to read WAREHOUSE.

Hope this helps.

Discuss

Discussion

Hi John,
about  this
Note that you will get 5 (not two) areas in red and below I've given you the reasons 
just  I  would  give  some  examples  but  not  all  unmatched items    to  understand  what  I want.
about  this 
Finally, I've corrected a typo in your headings (again) to read WAREHOUSE.

sorry again !
as  to  code  there  something  need  fixing  . if   change  or  add  new  data in sheet RP  should  update  automatically  in others  sheets  , but  this  doesn't  happen. for  instance   should  delete  the  color  if   change  items  in sheet RP  to  become  match  with  the  others which  were unmatched  in the  others  sheets  .
Hasson (rep: 30) Feb 21, '22 at 8:25 am
Hasson. So did the code do what the question asked?

I'm away from my PC now so can't make the extra change you requested above for a few hours yet (and note that it was not required in your original question- grrr! ).

Also, when you say "if change items  in sheet RP... match... others which  were unmatched", can I assume that you do NOT want to split combined values? (It would be harder to do / recognise split cells in sheets RP and proc1 when re-running the macro). 

Please clarify these points by editing your original question.
John_Ru (rep: 6152) Feb 21, '22 at 8:43 am
no  in this  case  I don't  split  cells   . just   highlight unmatched cells        this  macro  to  show  how  highlight unmatched  cells and  when  become  matched  based  on  sheet RP  , then  should  delete  the  color . this  file  is  not  relating  in  previous  file  to  split .   yes  this  is  the  same  file.  I  had  to  change  it  to  don't  misunderstand.   it was not required in your original question
I  expect   the  code  works  dynamically . that's  why  I  don't  mentioned. this  is  my  bad.  
Hasson (rep: 30) Feb 21, '22 at 9:04 am
Hasson

Please see my revised Answer (and file) then (hopefully) mark it as Selected.
John_Ru (rep: 6152) Feb 21, '22 at 1:29 pm
John
are  you  sure  about  new attached file? 
 did  you  test  it?
forgive  me,   it  seems    to  be  like  the  previous  file with  a little  different  by  show the Message Box from macro HighDict  . it  doesn't  delete  the  color .
Hasson (rep: 30) Feb 21, '22 at 2:00 pm
Hasson. Yes I'm sure about the new file and I did test it.

For example, if you change sheet RP  cell D11 to "ITA", the event macro is triggered and the revised HighByDict macro revises all sheets (as before) but now first deletes all the colour fills below row 1  before checking those cells against RP and applying red where it now applies (6 not 5 cells) including 2 cells starting QQW-10 BN.  What you get is "dynamic" revision of the file as RP is changed (and you don't need the button really).

I hope this is what you want since I don't intend to spend any more time on this (and, if not, you should now have sufficient clues to do whatever you really want, even if that wasn't described by your question).
John_Ru (rep: 6152) Feb 21, '22 at 3:34 pm
BTW your question said you would "truly  appreciate" help but that doesn't appear to be the case in the discussion do far! 
John_Ru (rep: 6152) Feb 21, '22 at 3:57 pm
BTW your question said you would "truly  appreciate" help but that doesn't appear to be the case in the discussion do far! 
this is not disrespect from me to appreciate your  help . Just I make sure all thing are good based on OP.
I don't intend to spend any more time on this
no  more  time  for  answer  this  question.   in  the  beginning  you  answered 99% of the  question   even  if  you  don't complete answering  the  last  part  of  the  question  . then  I  decided  select  answer  and  close  this  thread , but  this   depends  on  last  reply   what  you  decide. last  thing  because  the  items  are  similar  I  tought   the  code  doesn't  work  as  it  is . now  i  tested  more  than  one  time  and  works  perfectly.
my  apologies  if  my  question  is  not  clear  and  make  your  confused .  thanks  very  much  for  your  time  and  answering  .
Hasson (rep: 30) Feb 21, '22 at 4:52 pm
Glad it worked in the end, Hasson. Thanks for selecting my answer. 
John_Ru (rep: 6152) Feb 21, '22 at 6:24 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login