+ Reply to Thread
Results 1 to 2 of 2

Thread: Insert full row from worksheer to another base on condition

  1. #1
    Registered User
    Join Date
    02-04-2012
    Location
    canada
    MS-Off Ver
    Excel 2010
    Posts
    1

    Insert full row from worksheer to another base on condition

    HI There,

    I need your help with this issue:

    sheet1

    A B C
    1 JHON ON1 2
    2 FADI ON3 6
    3 BRIAN ON4 5
    4 MOHN ON9 2
    5 ALEX ON6 7

    sheet2

    A B C
    1 JHON ON1 2
    2 BRIAN ON4 7
    3 ALEX ON6 7
    4 FADI ON3 8

    I want sheet3 to be:
    A B C
    1 JHON ON1 2
    2 FADI ON3 6
    3 FADI ON3 8
    4 BRIAN ON4 5
    5 BRIAN ON4 7
    6 MOHN ON9 2
    7 ALEX ON6 7


    I want macro to compare betwwen sheet1 and sheet2,
    John & Alex were in both and same value in C col, copy to sheet three

    Fadi & Brian were in both but different value in C col, copy row to sheet3 and fill the row copied from sheet2 with "green for example"

    Mohn was only in sheet1, copy to sheet3

    I have attached the file

    Sorry being my question is long, but really I need help

    Thanks in advance

    Ziyadh
    Attached Files Attached Files

  2. #2
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,383

    Re: Insert full row from worksheer to another base on condition

    Hey Ziyadh,

    You can use this code -
    Option Explicit
    Dim lrow As Long
    Dim strow As Long
    Dim erow As Long
    Dim i As Long
    
    Sub compare_files()
    Application.ScreenUpdating = False
    
    Worksheets.Add(after:=Worksheets(Worksheets.Count)).Name = "Temp"
    lrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Worksheets("Sheet1").Range("A1:C" & lrow).Copy Worksheets("Temp").Range("A1")
    Worksheets("Temp").Range("D1:D" & lrow).Value = "1"
    
    lrow = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
    Worksheets("Sheet2").Range("A1:C" & lrow).Copy Worksheets("Temp").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
    strow = Worksheets("Temp").Range("D" & Rows.Count).End(xlUp).Row
    erow = Worksheets("Temp").Range("A" & Rows.Count).End(xlUp).Row
    Worksheets("Temp").Range("D" & strow + 1 & ":D" & erow).Value = "2"
    
    ActiveWorkbook.Worksheets("Temp").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Temp").Sort.SortFields.Add Key:=Range("A:A") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Temp").Sort
        .SetRange Range("A:D")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    With Worksheets("Temp")
        .Rows("1:1").Insert
        lrow = .Range("A" & Rows.Count).End(xlUp).Row
        For i = lrow To 2 Step -1
            If .Range("A" & i).Value = .Range("A" & i - 1).Value And .Range("B" & i).Value = .Range("B" & i - 1).Value And _
                .Range("C" & i).Value = .Range("C" & i - 1).Value And .Range("D" & i).Value <> .Range("D" & i + 1).Value Then
                .Rows(i & ":" & i).Delete
                lrow = lrow - 1
            ElseIf .Range("A" & i).Value = .Range("A" & i - 1).Value And .Range("B" & i).Value = .Range("B" & i - 1).Value And _
                .Range("C" & i).Value <> .Range("C" & i - 1).Value And .Range("D" & i).Value <> .Range("D" & i + 1).Value Then
                .Range("A" & i - 1 & ":C" & i).Interior.Color = 65535
            End If
        Next i
    End With
    
    Worksheets("Temp").Columns("D:D").Delete
    Application.ScreenUpdating = True
    
    End Sub
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0