PDA

View Full Version : Target.value



habets
05-09-2017, 02:00 PM
hi,
I tried to create automatic a new row under the target value but there is a error on the red marked tekst. I tried several thinks, like: > 0 or = "". But the problem stay.

What I'm doing wrong?

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Column <> 1 Then Exit Sub
If Target.Value <> "" Then
Rows(Target.Row + 1).EntireRow.Insert

End If
End Sub

p45cal
05-09-2017, 04:08 PM
You may want to do something like:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Column <> 1 Or Target.Cells.Count > 1 Then Exit Sub 'also aborts if more than one cell being changed at a time.
If Target.Value <> "" Then
Application.EnableEvents = False 'needed to stop your original error.
Rows(Target.Row + 1).EntireRow.Insert
Application.EnableEvents = True 'reinstate.
End If
End Sub

SamT
05-09-2017, 05:51 PM
I like P45cal's method, with one little addition

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Column <> 1 Or Target.Cells.Count > 1 Or Target = "" Then Exit Sub
Application.EnableEvents = False 'needed to stop your original error.

'Target.Row does not indicate which sheet. Target.Offset does

'Use
'Sh.Rows(Target.Row + 1).EntireRow.Insert
'Or
Target.Offset(1).EntireRow.Insert

Application.EnableEvents = True 'reinstate.
End Sub



Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Column > 1 Or Target.Cells.Count > 1 Or Target = "" Then Exit Sub
Application.EnableEvents = False 'needed to stop your original error.
Target.Offset(1).EntireRow.Insert
Application.EnableEvents = True 'reinstate.
End Sub