+ Reply to Thread
Results 1 to 3 of 3

Compare three worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    05-27-2011
    Location
    Virginia
    MS-Off Ver
    Excel 365
    Posts
    94

    Compare three worksheets

    Hi,

    I'm trying to compare three worksheets and output data to one of them, however the following code it not quite doing what I've expected. Please see if the following can to modified to assist with my problem.

    
    Sub MatchSheets()
        Dim wS As Worksheet, wT As Worksheet, wU As Worksheet
        Dim r1 As Range, r2 As Range, r3 As Range
        Dim cel1 As Range, cel2 As Range, cel3 As Range
    
        Set wS = ActiveWorkbook.Worksheets("Main")
        Set wT = ActiveWorkbook.Worksheets("Summary")
        Set wU = ActiveWorkbook.Worksheets("Archived")
    
        With wS
            Set r1 = .Range("B2", .Cells(.Rows.Count, .Columns("B:B").Column).End(xlUp))
        End With
    
        With wT
            Set r2 = .Range("E2", .Cells(.Rows.Count, .Columns("E:E").Column).End(xlUp))
        End With
        
        With wU
            Set r3 = .Range("F2", .Cells(.Rows.Count, .Columns("F:F").Column).End(xlUp))
        End With
    
        On Error Resume Next
        For Each cel1 In r1
            With Application
                Set cel2 = .Index(r2, .Match(cel1.Value, r2, 0)) 'find match in sheet2
                If Err = 0 Then
                   ' If cel1.Offset(, 6) <> cel2.Offset(, 6) Then copyRow cel2 'if difference, copy
                   '' If cel1.Offset(, 4) = cel2.Offset(, 4) Then cel2.Offset(, 4) = cel1.Offset(, 4) 'if difference, copy
                    If cel2.Offset(, 1) = cel3 & cel2.Offset(, 6) = cel3.Offset(, 7) & cel2.Offset(, 7) = cel3.Offset(, 8) Then  'cel2.Offset(, 4) = cel1.Offset(, 4) 'if difference, copy
                        cel1.Offset(, 10) = cel2.Offset(, 1).Value & " " & "No Progress"
                End If
                End If
                Err.Clear
            End With
        Next cel1
        Worksheets("Main").Activate
    End Sub
    Thank you in advance for your assistance.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,873

    Re: Compare three worksheets

    to start:
    1) cel3 is newer assigned, so (being Range type) it is permanently Nothing
    2) unlike in mathemetics, in VBA you cannot do
    if a=b=c then ...
    to check if three values are all equal. VBA will test a=b and result of this part will be boolean (either TRUE or FALSE) then this TRUE/FALSE will be compared with C. So you have to test
    if a=b and b=c then ...
    instead (of course from maths we remember that a=b and b=c implies a=c)

    may be 3) Have you tried to check what is assigned to cel2 after
    Set cel2 = .Index(r2, .Match(cel1.Value, r2, 0)) 'find match in sheet2
    ?
    As there is no test workbook I have not tested, but somehow I'm not sure what result will be returned here. Anyway - correct two above points first
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    05-27-2011
    Location
    Virginia
    MS-Off Ver
    Excel 365
    Posts
    94

    Re: Compare three worksheets

    Thanks Kaper I will provide a workbook as soon as I can.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Compare two worksheets and show the differences in two other worksheets....
    By raghuprabhu in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-26-2017, 12:25 AM
  2. [SOLVED] Compare two worksheets and highlight differences on one of the worksheets
    By Phil Payne in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-18-2013, 04:00 AM
  3. Compare Two worksheets
    By excelkeechak in forum Excel General
    Replies: 3
    Last Post: 04-12-2012, 08:31 AM
  4. Compare two worksheets
    By rageon75 in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 02-24-2006, 01:10 PM
  5. [SOLVED] How do I compare in between two worksheets?
    By korman in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  6. How do I compare in between two worksheets?
    By korman in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 10:05 PM
  7. How do I compare in between two worksheets?
    By korman in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-20-2005, 11:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1