PDA

View Full Version : Insert and Delete rows based on value



Trebby
08-03-2018, 11:22 AM
Hi All,

I have found a nice little code that allows me to insert a row when double clicking on a cell, copying contents as well. I was wondering if I could change it so it inserts a row if you click on the cell value '+' and deletes a row if you click on the value '-'? allowing me to insert and delete rows when needed.


Private Sub Worksheet_beforedoubleclick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Offset(1).EntireRow.Insert
Target.EntireRow.Copy Target.Offset(1).EntireRow
On Error Resume Next
Target.Offset(1).EntireRow.SpecialCells (xlConstants)
End Sub

Second question (I have no idea how this would work), how would I copy a range of rows (including contents) and insert them to the bottom of a section? Again using the same logic above but value 'I' Insert and 'D' to delete.

Thank you

p45cal
08-03-2018, 02:57 PM
Private Sub Worksheet_beforedoubleclick(ByVal Target As Range, Cancel As Boolean)
Select Case Target.Value
Case "+"
Cancel = True
Target.Offset(1).EntireRow.Insert
Target.EntireRow.Copy Target.Offset(1).EntireRow
On Error Resume Next
Target.Offset(1).EntireRow.SpecialCells (xlConstants) ' what does this do?
Case "-"
Cancel = True
Target.EntireRow.Delete
End Select
End Sub
?