Consulting

Results 1 to 3 of 3

Thread: Compare values of two range of cells and copy entire row

  1. #1
    VBAX Regular
    Joined
    Aug 2011
    Posts
    13
    Location

    Compare values of two range of cells and copy entire row

    Hello guys

    I have two sheets : the first one contains all the source data, the second one contains some of those data.
    I want to build a macro that compares the values of the first four cells in each row of sheet(1) with those of sheets (2) and if equal, then copies the entire row of sheets(2) into a sheet(3), else copies the entire row of sheets(1) into sheet(3).

    The rationale behind is the following : Data from sheet(1) are updated automatically from an independant database, and in sheet(2) I change manually some of the values (But I keep the values of the first four cells of each row so that I compare). Sheet(3) combines both.

    Below my macro : I don't know how to specify the range of cells to compare between sheet(1) and sheet(2) neither how to ask the macro compare those values. If equal then copy the entire row...

    Below Worksheets("Index_Div_Source") is sheet(1) and Worksheets("Index_Div_Manual") is sheet(2) . Sourcecel are the first four cells of each row in the sheet(1) and manualcel the first four cell of each row in sheet(3). Worksheets("Index_Div_Final") is sheet(3) where I want to combine everything.

    Hope it's clear,

    Thanks you


    [vba]Sub Compare()

    Dim i As Integer
    Dim manualcel As Range
    Dim sourcecel As Range

    Dim lastrow As Integer

    Application.ScreenUpdating = False

    lastrow = Worksheets("Index_Div_Source").Range("A65536").End(xlUp).Row


    With Worksheets("Index_Div_Final")

    For i = 3 To lastrow

    Set manualcel = Worksheets("Index_Div_Manual").Range("A" & i, "D" & i)

    For Each sourcecel In Worksheets("Index_Div_Source").Range("A" & i, "D" & i)

    If sourcecel.Value = manualcel.Value Then

    Worksheets("Index_Div_Manual").Range("A" & i).EntireRow.Copy
    Worksheets("Index_Div_Final").Range("A" & i).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    Else

    Worksheets("Index_Div_Source").Range("A" & i).EntireRow.Copy
    Worksheets("Index_Div_Final").Range("A" & i).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False


    End If

    Next

    Application.ScreenUpdating = True

    Next

    End With


    End Sub
    [/vba]
    Attached Files Attached Files

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.

    try below, and if it works, do not ask me how i've done that.

    [VBA]
    Sub CopyUniqueBasedOnFourCols()

    Dim a, i, j, k, n, b(), z, ws()
    Dim ws1 As Worksheet, ws2 As Worksheet

    Set ws2 = Sheets("Index_Div_Final")

    ReDim ws(1 To 2)
    ws(1) = "Index_Div_Manual"
    ws(2) = "Index_Div_Source"

    ReDim b(1 To 5000, 1 To 17)
    With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For k = 1 To 2
    Set ws1 = Sheets(ws(k))
    a = ws1.Range("A2:Q" & ws1.Cells(Rows.Count, 1).End(xlUp).Row).Value
    For i = 1 To UBound(a, 1)
    If Not IsEmpty(a(i, 2)) Then
    z = a(i, 1) & ":" & a(i, 2) & ":" & a(i, 3) & ":" & a(i, 4)
    If Not .exists(z) Then
    n = n + 1
    For j = 1 To 17
    b(n, j) = a(i, j)
    Next
    .Add z, n
    End If
    End If
    Next i
    Set ws1 = Nothing
    Next k
    End With

    ws2.Range("A2:Q5000").ClearContents
    ws2.Range("A2").Resize(n, 17).Value = b

    Set ws2 = Nothing

    End Sub
    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    edit:
    requires a reference to Microsoft Scripting Runtime
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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