PDA

View Full Version : Toggle newly inserted row based on criteria



VT588
12-03-2015, 08:48 AM
I’ve been looking and haven’t found anything or I am not properly wording my search query…
What I’m looking to do:
Based upon criteria in a cell [a Marlette double-click] insert an entire row underneath this cell.
…but that same row [the just inserted row] would be removed IF the check is removed.
This is a self-correcting procedure - if the user accidently places the check in the wrong row/cell.
Obviously, the user could just delete that incorrectly inserted row but that's prone to more problems – like deleting the wrong row, etc…

Thanks for any ideas...

mikerickson
12-03-2015, 07:07 PM
Something like this should work.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
With Target
If .Column = 3 Then
If .Value = "X" Then
.Offset(1, 0).EntireRow.Delete
.Value = vbNullString
Else
.Offset(1, 0).EntireRow.Insert
.Value = "X"
End If
End If
End With
End Sub
Or are you looking for a test to see if the row that is about to be deleted is recently added.
This will delete the row only if there is no entry in the row.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
With Target
If .Column = 3 Then
If .Value = "X" Then
With .Offset(1, 0).EntireRow
If WorksheetFunction.CountA(.Cells) = 0 Then .Delete
End With
.Value = vbNullString
Else
.Offset(1, 0).EntireRow.Insert
.Value = "X"
End If
End If
End With
End Sub