+ Reply to Thread
Results 1 to 17 of 17

Copy Row of Highlighted Cell

Hybrid View

  1. #1
    Registered User
    Join Date
    07-10-2017
    Location
    Philippines
    MS-Off Ver
    MS OFFICE 2016
    Posts
    38

    Question Copy Row of Highlighted Cell

    I've been comparing 2 sheets and then highlight differences, what I want to do is, how do I copy the entire row that there is a highlighted cell to another sheet?
    Can you help me work this out?
    Thank you

    Here's my code on comparing

    Sub compareSheets(shtSheet1 As String, shtSheet2 As String)
    Dim mycell As Range
    
    
    'For each cell in sheet2 that is not the same in Sheet1, color it yellow
    For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
    If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.row, mycell.Column).Value Then
    
    mycell.Interior.Color = vbYellow
    
    End If
    Next
    
    'For each cell in sheet2 that is not the same in Sheet1, color it yellow
    For Each mycell In ActiveWorkbook.Worksheets(shtSheet1).UsedRange
    If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet2).Cells(mycell.row, mycell.Column).Value Then
    
    mycell.Interior.Color = vbYellow
    
    End If
    Next
    
    ActiveWorkbook.Sheets(shtSheet2).Select
    
    
    End Sub

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Copy Row of Highlighted Cell

    I would think adding some code after your highlight code...i.e.

    Dim nRow as Long
    nRow = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row + 1'Sheet2 the name of sheet you want to copy to
    mycell.EntireRow.Copy Sheet2.Range("A" & nRow).PasteSpecial xlPasteValues
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    07-10-2017
    Location
    Philippines
    MS-Off Ver
    MS OFFICE 2016
    Posts
    38

    Re: Copy Row of Highlighted Cell

    Quote Originally Posted by sintek View Post
    I would think adding some code after your highlight code...i.e.

    Dim nRow as Long
    nRow = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row + 1'Sheet2 the name of sheet you want to copy to
    mycell.EntireRow.Copy Sheet2.Range("A" & nRow).PasteSpecial xlPasteValues
    It says invalid qualifier even though I changed it to shtSheet2 which is my Sheet2.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy Row of Highlighted Cell

    Maybe:

    Sub asilocyra()
    Dim mycell As Range
    
    
    'For each cell in sheet2 that is not the same in Sheet1, color it yellow
    For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
    If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.row, mycell.Column).Value Then
    
    mycell.Interior.color = vbYellow
    Rows(mycell.row).Copy Sheets(shtSheet1).Cells(mycell.row, 1)
    
    End If
    Next
    
    'For each cell in sheet2 that is not the same in Sheet1, color it yellow
    For Each mycell In ActiveWorkbook.Worksheets(shtSheet1).UsedRange
    If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet2).Cells(mycell.row, mycell.Column).Value Then
    
    mycell.Interior.color = vbYellow
    Rows(mycell.row).Copy Sheets(shtSheet2).Cells(mycell.row, 1)
    End If
    Next
    
    ActiveWorkbook.Sheets(shtSheet2).Select
    End Sub

  5. #5
    Registered User
    Join Date
    07-10-2017
    Location
    Philippines
    MS-Off Ver
    MS OFFICE 2016
    Posts
    38

    Re: Copy Row of Highlighted Cell

    Working on it. just wondering how to paste that copy code

  6. #6
    Registered User
    Join Date
    07-10-2017
    Location
    Philippines
    MS-Off Ver
    MS OFFICE 2016
    Posts
    38

    Re: Copy Row of Highlighted Cell

    Quote Originally Posted by JOHN H. DAVIS View Post
    Maybe:

    Sub asilocyra()
    Dim mycell As Range
    
    
    'For each cell in sheet2 that is not the same in Sheet1, color it yellow
    For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
    If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.row, mycell.Column).Value Then
    
    mycell.Interior.color = vbYellow
    Rows(mycell.row).Copy Sheets(shtSheet1).Cells(mycell.row, 1)
    
    End If
    Next
    
    'For each cell in sheet2 that is not the same in Sheet1, color it yellow
    For Each mycell In ActiveWorkbook.Worksheets(shtSheet1).UsedRange
    If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet2).Cells(mycell.row, mycell.Column).Value Then
    
    mycell.Interior.color = vbYellow
    Rows(mycell.row).Copy Sheets(shtSheet2).Cells(mycell.row, 1)
    End If
    Next
    
    ActiveWorkbook.Sheets(shtSheet2).Select
    End Sub
    Thank you for the suggestion, but for some reason it doesn't work.

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Copy Row of Highlighted Cell

    Please upload a sample workbook so that we can see your exact scenario....

  8. #8
    Registered User
    Join Date
    07-10-2017
    Location
    Philippines
    MS-Off Ver
    MS OFFICE 2016
    Posts
    38

    Re: Copy Row of Highlighted Cell

    Quote Originally Posted by sintek View Post
    Please upload a sample workbook so that we can see your exact scenario....
    Here's my sample.
    https://drive.google.com/open?id=0B4...0VjWXQzTmN6ZVE

  9. #9
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Copy Row of Highlighted Cell

    I noticed that there are occurrences of more than one highlighted cell in a row, so i am guessing that you want that row copied over only one...Give this a try...

    Sub compareSheets(shtSheet1 As String, shtSheet2 As String, shtSheet3 As String)
    Dim mycell As Range
    Dim LastLine As Long, RowNum As Long
    Dim nRow As Long
    nRow = Sheets(shtSheet3).Cells(Rows.Count, "A").End(xlUp).Row + 1
    With Sheets(shtSheet1)
        .Activate
        For Each mycell In .UsedRange
            If Not mycell.Value = Sheets(shtSheet2) _
                .Cells(mycell.Row, mycell.Column).Value Then
                mycell.Interior.Color = vbYellow
                If mycell.Row = RowNum Then GoTo nxt1:
                    RowNum = mycell.Row
                    Rows(mycell.Row).Copy Sheets(shtSheet3).Cells(nRow, 1)
                    nRow = nRow + 1
                End If
    nxt1:
        Next
    End With
    
    With Sheets(shtSheet2)
        .Activate
        For Each mycell In .UsedRange
            If Not mycell.Value = Sheets(shtSheet1) _
                .Cells(mycell.Row, mycell.Column).Value Then
                mycell.Interior.Color = vbYellow
                If mycell.Row = RowNum Then GoTo nxt2:
                    RowNum = mycell.Row
                    Rows(mycell.Row).Copy Sheets(shtSheet3).Cells(nRow, 1)
                    nRow = nRow + 1
                End If
    nxt2:
        Next
    End With
    End Sub

  10. #10
    Registered User
    Join Date
    07-10-2017
    Location
    Philippines
    MS-Off Ver
    MS OFFICE 2016
    Posts
    38

    Re: Copy Row of Highlighted Cell

    Quote Originally Posted by sintek View Post
    i noticed that there are occurrences of more than one highlighted cell in a row, so i am guessing that you want that row copied over only one...give this a try...

    sub comparesheets(shtsheet1 as string, shtsheet2 as string, shtsheet3 as string)
    dim mycell as range
    dim lastline as long, rownum as long
    dim nrow as long
    nrow = sheets(shtsheet3).cells(rows.count, "a").end(xlup).row + 1
    with sheets(shtsheet1)
        .activate
        for each mycell in .usedrange
            if not mycell.value = sheets(shtsheet2) _
                .cells(mycell.row, mycell.column).value then
                mycell.interior.color = vbyellow
                if mycell.row = rownum then goto nxt1:
                    Rownum = mycell.row
                    rows(mycell.row).copy sheets(shtsheet3).cells(nrow, 1)
                    nrow = nrow + 1
                end if
    nxt1:
        Next
    end with
    
    with sheets(shtsheet2)
        .activate
        for each mycell in .usedrange
            if not mycell.value = sheets(shtsheet1) _
                .cells(mycell.row, mycell.column).value then
                mycell.interior.color = vbyellow
                if mycell.row = rownum then goto nxt2:
                    Rownum = mycell.row
                    rows(mycell.row).copy sheets(shtsheet3).cells(nrow, 1)
                    nrow = nrow + 1
                end if
    nxt2:
        Next
    end with
    end sub

    this works perfectly from what i wanted to happen! Thank you!
    already added reputation to you!

    Thanks again!

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Copy Row of Highlighted Cell

    Glad i could help..Tx for rep +

  12. #12
    Registered User
    Join Date
    07-10-2017
    Location
    Philippines
    MS-Off Ver
    MS OFFICE 2016
    Posts
    38

    Re: Copy Row of Highlighted Cell

    Quote Originally Posted by sintek View Post
    Glad i could help..Tx for rep +
    After trying this again, I noticed that when moving the row with highlighted cell it only copies one highlighted cell.

    Please see attached photo to see what I mean.

    Thank you!
    Attached Images Attached Images

  13. #13
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Copy Row of Highlighted Cell

    I don't understand...What seems to be the problem...

  14. #14
    Registered User
    Join Date
    07-10-2017
    Location
    Philippines
    MS-Off Ver
    MS OFFICE 2016
    Posts
    38

    Re: Copy Row of Highlighted Cell

    Quote Originally Posted by sintek View Post
    I don't understand...What seems to be the problem...
    shtSheet1 and shtSheet2 are the two sheet that we're comparing and put the highlighted row to the shtSheet
    but after comparing, it seems to be that in every row there is only one highlighted cell.

    I.e, shtSheet1, A3 to E3 are highlighted but when transferred to shtSheet3 it only shows ONE highlighted cell which has to be two (name and course)

  15. #15
    Registered User
    Join Date
    07-10-2017
    Location
    Philippines
    MS-Off Ver
    MS OFFICE 2016
    Posts
    38

    Re: Copy Row of Highlighted Cell

    Quote Originally Posted by sintek View Post
    I don't understand...What seems to be the problem...
    do you think this has to do something with pretty big datas?

  16. #16
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Copy Row of Highlighted Cell

    Can you upload a smaller sample...Not that monster 36mb file..
    What happens when you change this
    Rows(mycell.Row).Copy Sheets(shtSheet3).Cells(nRow, 1)
    to this
    Rows(mycell.Row).Copy
    Sheets(shtSheet3).Cells(nRow, 1).PasteSpecial xlPasteAll
    Do for both ranges...
    Last edited by Sintek; 07-25-2017 at 03:44 AM.

  17. #17
    Registered User
    Join Date
    07-10-2017
    Location
    Philippines
    MS-Off Ver
    MS OFFICE 2016
    Posts
    38

    Re: Copy Row of Highlighted Cell

    Quote Originally Posted by sintek View Post
    Can you upload a smaller sample...Not that monster 36mb file..
    What happens when you change this
    Rows(mycell.Row).Copy Sheets(shtSheet3).Cells(nRow, 1)
    to this
    Rows(mycell.Row).Copy
    Sheets(shtSheet3).Cells(nRow, 1).PasteSpecial xlPasteAll
    Do for both ranges...
    I've already fixed it by adding this:

    With Sheets(shtSheet1)
        .Activate
        For Each mycell In .UsedRange
            If Not mycell.Value = Sheets(shtSheet2) _
                .Cells(mycell.Row, mycell.Column).Value Then
                mycell.Interior.Color = vbYellow
                If mycell.Row = RowNum And mycell.Interior.Color = vbYellow Then GoTo nxt1:
                    
                End If
    nxt1:
        Next
    End With
    Thank you!

+ 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. Copy Highlighted Cell to Another Worksheet
    By spencerp237 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-23-2017, 03:15 PM
  2. simple one: If cell highlighted, then copy value
    By hmm321 in forum Excel General
    Replies: 4
    Last Post: 10-26-2015, 02:24 AM
  3. [SOLVED] Copy entire row if cell is highlighted
    By bdrilling33 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 09-02-2014, 10:13 AM
  4. Copy Highlighted Cell from one tab to another
    By nhunt in forum Excel General
    Replies: 3
    Last Post: 10-31-2013, 02:16 AM
  5. Copy Highlighted Cell And Paste
    By samdegg in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 08-28-2013, 06:23 PM
  6. Copy a Row to another sheet of a cell in a column is highlighted
    By kdsanderson in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-30-2013, 11:52 AM
  7. Replies: 2
    Last Post: 12-05-2012, 09:54 AM

Tags for this Thread

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