Excel VBA - Comparison two sheets

NeewBie

New Member
Joined
Aug 7, 2012
Messages
40
Hi,

I have this script to compare:

'Option Explicit

'Sub FindDuplicate()


'Dim LastRow As Long
'Dim MyRg1 As Range, MyRg2 As Range
'Dim A As Range
'Dim F
' Application.ScreenUpdating = False
'LastRow = Range("a" & Rows.Count).End(xlUp).Row
'Range("B1:B" & LastRow).ClearContents
'Set MyRg1 = Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp))
'With Sheets("Sheet2")
'Set MyRg2 = .Range(.Cells(2, "A"), .Cells(Rows.Count, "A").End(xlUp))
'End With
'With MyRg2
'For Each A In MyRg1
'If (A <> Empty) Then
'Set F = .Find(What:=A.Value, After:=.Cells(1, 1), LookIn:=xlValues, _
'LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
'MatchCase:=False)
'If (Not F Is Nothing) Then
'A.Offset(0, 1) = .Cells(F.Row, F.Column + 1)
'End If
'End If
'Next A
' End With
'Application.ScreenUpdating = True

'End Sub

What I cant it to do is to colorize (if its works) when it find dublicates or that the script write all dublicates in worksheet 3. I also want it so search in the whole range in sheet 1 and sheet 2. Someone that can helps me?

Thank you!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Code:
Sub FindDuplicate()
Dim LastRow As Long
Dim MyRg1 As Range, MyRg2 As Range
Dim A As Range
Dim F
' Application.ScreenUpdating = False
LastRow = Range("a" & Rows.Count).End(xlUp).Row
Range("B1:B" & LastRow).ClearContents
Set MyRg1 = Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp))
With Sheets(2)
Set MyRg2 = .Range(.Cells(2, "A"), .Cells(Rows.Count, "A").End(xlUp))
End With
With MyRg2
  For Each A In MyRg1
    If (A <> Empty) Then
      Set F = .Find(What:=A.Value, After:=.Cells(1, 1), LookIn:=xlValues, _
      LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
      MatchCase:=False)
      If (Not F Is Nothing) Then
        A.Font.Bold = True
      End If
    End If
  Next A
End With
'Application.ScreenUpdating = True
End Sub
 
Upvote 0
Code:
Sub FindDuplicate()
Dim LastRow As Long
Dim MyRg1 As Range, MyRg2 As Range
Dim A As Range
Dim F
' Application.ScreenUpdating = False
LastRow = Range("a" & Rows.Count).End(xlUp).Row
Range("B1:B" & LastRow).ClearContents
Set MyRg1 = Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp))
With Sheets(2)
Set MyRg2 = .Range(.Cells(2, "A"), .Cells(Rows.Count, "A").End(xlUp))
End With
With MyRg2
  For Each A In MyRg1
    If (A <> Empty) Then
      Set F = .Find(What:=A.Value, After:=.Cells(1, 1), LookIn:=xlValues, _
      LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
      MatchCase:=False)
      If (Not F Is Nothing) Then
        A.Font.Bold = True
      End If
    End If
  Next A
End With
'Application.ScreenUpdating = True
End Sub

Thank you for your answer but I think you missunderstood me. I dont want it to delete any values in sheet 1 or sheet 2. I want it do colorize OR write all dublicates in sheet 3 alternative write all non dublicates in sheet 3.

Greatful for all help!
 
Upvote 0
did you test it ? my macro doesn't delete any values in sheet 1 or sheet 2.
Where did you find any command for deleting ?
 
Upvote 0
Ofcorse i test it my friend. I wrote values in column A and B in both sheet when I was debugging it the values in sheet A was deleted, only the values that dont was dublicate.
 
Upvote 0
Hi, NeewBie,

you delivered the code with the following line of code

Code:
Range("B1:B" & LastRow).ClearContents
This will clear all Contents from Column B. So if you want to see how the code works you either use column C for your check or put an ' (or REM) to the beginning of the above mentioned line which will not execute as it then will be treated as a comment.

@patel45:
you know for sure that the active sheet in the workbook is not the second sheet in the workbook? I pretty much doubt that as you ask for a link. Please be more precise about how to reference sheets considering their index, their tab names, their codenames.

Ciao,
Holger
 
Upvote 0
@patel45:
you know for sure that the active sheet in the workbook is not the second sheet in the workbook? I pretty much doubt that as you ask for a link. Please be more precise about how to reference sheets considering their index, their tab names, their codenames.
I know for "sure" only that the user wrote, you know very well that I love links, then instead of externalize doubts about my requests provide your solution.
 
Last edited:
Upvote 0
Hi, patel45,

you simplified the code from
Code:
With Sheets("Sheet2")
to
Code:
With Sheets(2)
Had you used
Code:
With Worksheets(2)
you would have excluded any posssible error (I´m not saying there will be but might be due to the different collections of both items).

then instead of externalize doubts about my requests provide your solution
There was a concern regarding the code which I explained. And I mentioned there might be an error when using Sheets(2).

patel45 #4 said:
did you test it ? my macro doesn't delete any values in sheet 1 or sheet 2.
Where did you find any command for deleting ?
Both in the opening post as well as your answer:
patel45 #2 said:
Range("B1:B" & LastRow).ClearContents


Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,406
Members
448,958
Latest member
Hat4Life

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