reader6886
New Member
- Joined
- Sep 10, 2020
- Messages
- 25
- Office Version
- 2019
- Platform
- 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
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
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