Help With On Sheet 2 Not On Sheet 1 Code

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I have the code below that has worked fine for some time but now it is causing me problems on a certain WB. It highlights in red what is on sheet 2 but not on sheet 1. I am using it now but it is highlighting certain numbers that are on both sheets. These come in the form of years or 4 digit numbers i.e 1997, 2008 etc..

It only does it with some and not all. Can someone help please.

Code:
Sub OnSheet2NotSheet1()
Dim LR1&, lc1&, LR2&, lc2&
Dim d As Object, a1, a2, e, i&, j&
With Sheets("sheet1")
LR1 = .Cells.Find("*", After:=.Cells(1), SearchOrder:=xlByRows, _
    searchdirection:=xlPrevious).Row
lc1 = .Cells.Find("*", After:=.Cells(1), SearchOrder:=xlByColumns, _
    searchdirection:=xlPrevious).Column
a1 = .Cells(1).Resize(LR1, lc1)
End With
With Sheets("sheet2")
LR2 = .Cells.Find("*", After:=.Cells(1), SearchOrder:=xlByRows, _
    searchdirection:=xlPrevious).Row
lc2 = .Cells.Find("*", After:=.Cells(1), SearchOrder:=xlByColumns, _
    searchdirection:=xlPrevious).Column
a2 = .Cells(1).Resize(LR2, lc2)
End With
Set d = CreateObject("scripting.dictionary")
d.comparemode = 1
For Each e In a1: d(e) = 1: Next e
For i = 1 To LR2: For j = 1 To lc2
    If d(a2(i, j)) <> 1 Then _
        Sheets("sheet2").Cells(i, j).Interior.ColorIndex = 3
Next j, i
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I forgot to add I have done copy>pastespecial>values etc.. just in case.
 
Upvote 0
I have the code below that has worked fine for some time but now it is causing me problems on a certain WB. It highlights in red what is on sheet 2 but not on sheet 1. I am using it now but it is highlighting certain numbers that are on both sheets. These come in the form of years or 4 digit numbers i.e 1997, 2008 etc..

It only does it with some and not all. Can someone help please.

Rich (BB code):
Sub OnSheet2NotSheet1()
Dim LR1&, lc1&, LR2&, lc2&
Dim d As Object, a1, a2, e, i&, j&
With Sheets("sheet1")
LR1 = .Cells.Find("*", After:=.Cells(1), SearchOrder:=xlByRows, _
    searchdirection:=xlPrevious).Row
lc1 = .Cells.Find("*", After:=.Cells(1), SearchOrder:=xlByColumns, _
    searchdirection:=xlPrevious).Column
a1 = .Cells(1).Resize(LR1, lc1)
End With
With Sheets("sheet2")
LR2 = .Cells.Find("*", After:=.Cells(1), SearchOrder:=xlByRows, _
    searchdirection:=xlPrevious).Row
lc2 = .Cells.Find("*", After:=.Cells(1), SearchOrder:=xlByColumns, _
    searchdirection:=xlPrevious).Column
a2 = .Cells(1).Resize(LR2, lc2)
End With
Set d = CreateObject("scripting.dictionary")
d.comparemode = 1
For Each e In a1: d(e) = 1: Next e
For i = 1 To LR2: For j = 1 To lc2
    If d(a2(i, j)) <> 1 Then _
        Sheets("sheet2").Cells(i, j).Interior.ColorIndex = 3
Next j, i
End Sub
Hi,

Do you mean that the highlighting appears on both sheets?

And (assuming the code can be made OK otherwise) is it only numbers (specifically integers) that you are interested in, or may you want text highlighted as well?

Specifically on the code you post, problem may possibly arise from the particular use of the scripting dictionary object. The way it's used there may fall down in some circumstances.

A modification of the above code that might work for you is:
replace the lines in red by
Rich (BB code):
For Each e In a1
    If Not d.exists(e) Then d.Add e, Empty
Next e

For i = 1 To LR2: For j = 1 To lc2
    If Not d.exists(a2(i, j)) Then _
        Sheets("sheet2").Cells(i, j).Interior.ColorIndex = 3
If the wrong elements are highlighted, try deleting the "Not" in red and see how that goes.
 
Upvote 0
No, the code highlights only on sheet 2 what is on that sheet and not on sheet 1. What it looks for is anything, text, integer, mixture of both etc.. The code I posted has been problem free for ages so I don't understand why it is playing up now. It is specifically numbers it is doing it on.
 
Upvote 0
Tried what you suggested and it is still doing it.
 
Upvote 0
I can't see that there's anything about with code that might cause that sort of problem.

Are the numbers indeed the same?

Are both of them numbers and not (say) one is the result of some formula.

Do they both format the same of you copy and paste to a new sheet?

Can you do arithmetic operations equally well with both? Like do they add together OK to give the expected result.

Without seeing an example of the situation you describe, and not knowing how your data are generated for each worksheet (I don't want to know) it's hard to be more specific.

I came across a situation a week or so back on this forum where I solved the OP's problem for the data as it was posted here. But there was seemingly a problem when the method was applied to the original data, which seemingly had some features that were filtered out by Jeannie or whatever posting process was used.
 
Upvote 0
The numbers are a result of a Vlookup which is why I paste specialed values etc. Although most if not all do have a little green mark in the corner of the cell.
 
Upvote 0
Click on a cell with the small green triangle.

There should appear next to it a small box with exclamation mark in it.

Click on that and note the options. They may tell you what the cause of the problem is.

e.g. if it's "number stored as text" you may then have the option of converting this to number. Up to you whether you want to do this or not.
 
Upvote 0
Click on a cell with the small green triangle.

There should appear next to it a small box with exclamation mark in it.

Click on that and note the options. They may tell you what the cause of the problem is.

e.g. if it's "number stored as text" you may then have the option of converting this to number. Up to you whether you want to do this or not.

Thanks, that done it. I selected them all then converted to number and it worked.
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,865
Members
452,948
Latest member
UsmanAli786

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