Refacor my code please

emukiss10

Board Regular
Joined
Nov 17, 2017
Messages
201
hello, I need to temper this code a little (code made by Fluff)

Code:
   Dim Xl As Range
   Dim RngX As Range
   Dim ValU As String
  
  With CreateObject("scripting.dictionary")
      For Each Xl In Sheets(1).Range("A2", Sheets(1).Range("A" & Rows.count).End(xlUp))
         ValU = Xl.Value & Xl.Offset(, 5).Value
         If Not .Exists(ValU) Then .Add ValU, Nothing
      Next Xl
      For Each Xl In Sheets(2).Range("A2", Sheets(2).Range("A" & Rows.count).End(xlUp))
         ValU = Xl.Value & Xl.Offset(, 16).Value
         If Not .Exists(ValU) Then
           Xl.Offset(, 0).Interior.Color = RGB(157, 157, 157)
           Xl.Offset(, 1).Interior.Color = RGB(230, 130, 130)
         End If
      Next Xl
   End With

it compare values 1 to 1 from different sheets based on column A1.

In offset(0,5) sheet 1 and offset(0,16) sheet 2 it compares values and it works great but I need a little change.

everything should be 1:1 but if in Sheet 1 value that is currently checked is 14 and sheet 2 value is 12 it should be ignored (as correct - just like 1:1 match) any other result than this or 1:1 match should be noted (in this case by colour).

Can You make this work?

best regards!
W.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Your code colours cells in columns A and B of Sheets(2) if the concatenated value of columns A and Q doesn't occur anywhere in the concatenated values of columns A and F in Sheets(1).

What do you mean exactly when you say "sheet2 value is 12"? Are you perhaps saying that if the column A value is 12 you don't want to colour the cells? Or is this perhaps the column Q value?

And what do you mean by "Sheet 1 value that is currently checked is 14"? There is no 1:1 comparison happening in the code.
 
Upvote 0
Ill try to explain

This code is looking for the same values in both sheets. If values does not match it is alter the color of that cells.
I would like to make a little change. IF values do not match - change color - except value 14 sheet 1 and value 12 sheet 2 - This should not be marked.

values Ex.

Sheet1 - 10, Sheet2 - 10 | ok
Sheet1 - 14, Sheet2 - 12 | ok
Sheet1 - 12, Sheet2 - 14 | NOT OK
Sheet1 - 2, Sheet2 - 2 | ok
Sheet1 - 55, Sheet2 - 54 | NOT OK
Sheet1 - 23, Sheet2 - 22 | NOT OK
Sheet1 - 88, Sheet2 - 88 | ok
Sheet1 - 14, Sheet2 - 12 | ok
Sheet1 - 14, Sheet2 - 11 | NOT OK



SHEET 1

name1xxx12 - OK
name2xxx14 - OK (no color)
name3xxx24 - not ok
name4xxx21 - ok
name5xxx13 - not ok
name6xxx12 - ok
name7xxx19 - ok
name8xxx12 - not ok

<tbody>
</tbody>

SHEET2
name1xxx12 - OK
name2xxx12 - OK (no color)
name3xxx22 - not ok
name4xxx21 - ok
name5xxx14 - not ok
name6xxx12 - ok
name7xxx19 - ok
name8xxx14 - not ok

<tbody>
</tbody>
 
Last edited:
Upvote 0
Here's one way, just tweaking your existing code:

Code:
Dim Xl As Range
Dim RngX As Range
Dim ValU As String

With CreateObject("scripting.dictionary")
For Each Xl In Sheets(1).Range("A2", Sheets(1).Range("A" & Rows.Count).End(xlUp))
    On Error Resume Next
        .Add Xl.Value & Xl.Offset(, 5).Value, Nothing
        If Xl.Offset(, 5).Value = 14 Then .Add Xl.Value & 12, Nothing   'assumes numeric 14, not string?
    On Error GoTo 0
Next Xl
For Each Xl In Sheets(2).Range("A2", Sheets(2).Range("A" & Rows.Count).End(xlUp))
    ValU = Xl.Value & Xl.Offset(, 16).Value
    If Not .Exists(ValU) Then
        Xl.Offset(, 0).Interior.Color = RGB(157, 157, 157)
        Xl.Offset(, 1).Interior.Color = RGB(230, 130, 130)
    End If
    Next Xl
End With


Excel 2010
AF
1
2Name1xx
3Name2xx
4Name3xx
5Name414
6Name5xx
7Name6xx
8Name712
9Name8xx
Sheet1



Excel 2010
ABQR
1Comment
2Name1yyNot found
3AnotherNamexxNot found
4Name2xxFound
5Name412Name4 / 14 found on Sheet1, therefore accept
6AnotherName12Not found
7Name212Neither Name2 / 12 nor Name2 / 14 exist on Sheet1
8Name3xxFound
9Name712Found
Sheet2
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,360
Messages
6,130,175
Members
449,562
Latest member
mthrasher16

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