Comparing rows in two sheets

reader6886

New Member
Joined
Sep 10, 2020
Messages
25
Office Version
  1. 2019
Platform
  1. Windows
Hi . I have managed to put in a vba code for my task , learning from here and there. what it does is.
1.when I click on a cell on a sheet 1 it copies the whole cell row on sheet 2.
2. It renames sheet 2 dynamically with info ( copied row from sheet 1 and used row on sheet 2 ) which is very helpful.

what i am trying to do is add if then else statement so that.
1.when i click a cell on sheet 1 . it first checks if the active cell row value is already on sheet 2 or not.
a if it is it gives " already added" message .
b. if not it copies whole active cell row from sheet 1 to sheet 2 like usual

what would be working the expression to carry this out. what do i need to put in the bold line .below
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

    
    Dim sh As Worksheet, shnext As Worksheet
    
 
    Set sh = ActiveSheet
    Set shnext = sh.Next




[B]
If ActiveCell.EntireRow.Value matches with row on shnext  Then[/B]
MsgBox(" already there")

Else

ActiveCell.EntireRow.Copy _

shnext.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues

End If



   Dim LR As Long
 

  LR = shnext.Cells(Rows.Count, 1).End(xlUp).Row

   shnext.Name = ActiveCell.Row & "=" & LR


Application.CutCopyMode = False



End Sub

Other "feature" I wanted to add is if I wanted to delete the row on sheet 2 after it has been added i just need to click it. I did manage to make the following code work by putting it in sheet 2 . But it deletes the row from sheet 1 too . I want the delete happening in sheet 2 only not sheet 1 . What am i doing wrong . What do i need to change

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)



ActiveCell.EntireRow.Delete _


End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi,

Created below based on some assumptions. Please check.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

    Dim sh As Worksheet, shnext As Worksheet
    Dim lRow As Integer, found As String, rowno As Integer
    
    Set sh = ActiveSheet
    Set shnext = sh.Next
    
    lRow = shnext.Cells(Rows.Count, 1).End(xlUp).Row
    found = "n"
    
    For rowno = 1 To lRow
        If ActiveCell.Value = shnext.Cells(rowno, ActiveCell.Column) Then
            MsgBox ("Already there")
            found = "y"
            Exit For
        End If
    Next
    
    If found = "n" Then
        ActiveCell.EntireRow.Copy shnext.Cells(lRow + 1, 1)
    End If

    Dim LR As Long
     
    LR = shnext.Cells(Rows.Count, 1).End(xlUp).Row
    shnext.Name = ActiveCell.Row & "=" & LR
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Solution
Hi,

Created below based on some assumptions. Please check.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

    Dim sh As Worksheet, shnext As Worksheet
    Dim lRow As Integer, found As String, rowno As Integer
   
    Set sh = ActiveSheet
    Set shnext = sh.Next
   
    lRow = shnext.Cells(Rows.Count, 1).End(xlUp).Row
    found = "n"
   
    For rowno = 1 To lRow
        If ActiveCell.Value = shnext.Cells(rowno, ActiveCell.Column) Then
            MsgBox ("Already there")
            found = "y"
            Exit For
        End If
    Next
   
    If found = "n" Then
        ActiveCell.EntireRow.Copy shnext.Cells(lRow + 1, 1)
    End If

    Dim LR As Long
    
    LR = shnext.Cells(Rows.Count, 1).End(xlUp).Row
    shnext.Name = ActiveCell.Row & "=" & LR
    Application.CutCopyMode = False
End Sub
THANK YOU SO SO MUCH . works great . happy holidays and happy new year .
 
Upvote 0
Great. Thanks for the feedback.

Happy new year.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top