Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a macro in vba to update information on sheet two from sheet one. The individual records are tied together by keys. So, one key corresponds to a record in sheet 1 and also a record in sheet two. I have the macro start off by defining the rows that are filled with data (at this stage it only has the a column). Then it enters a for loop and gets the entire rows and compares the two rows and updates them if they aren't the same. I am not sure how I would write the line for the if statement to compare the two rows that have the same key (since they aren't going to be in sequential order). Any help would be greatly appreciated. I have posted the code that I have written so far below.


VB
 Sub crossUpdate()
    Dim rng1 As Range, rng2 As Range, N As Long
    Dim rng1Row As Range
    Dim rng2Row As Range
    Dim i As Integer, j As Integer
    Dim Cell As Variant
    
    
    N = Cells(Rows.Count, "A").End(xlUp).row
    Set rng1 = Sheet1.Cells.Range("A2:A" & N)
    Set rng2 = Sheet2.Cells.Range("A2:A" & N)
    
    
    For i = 2 To rng1.Rows.Count
        Set rng1Row = rng1.Cells(i, 1).EntireRow
        Set rng2Row = rng2.Cells(i, 1).EntireRow
        Key = Sheet1.Range("A" & i)
            For j = 1 To rng1.Columns.Count
                If rng1Row.Cells(i, j).Value <> rng2Row.Cells(i, j).Value Then
                    
                 Else
                    
                End If
            Next j
    Next i
    
    End Sub
Posted
Comments
LLLLGGGG 25-Sep-14 15:56pm    
You may use a loop to compare Keys before, then check if the records are the same, fianlly, copy the ones who aren't...

LG
Maciej Los 6-Oct-14 14:16pm    
Please, post sample data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900