Consulting

Results 1 to 3 of 3

Thread: Target.value

  1. #1
    VBAX Newbie
    Joined
    May 2017
    Posts
    1
    Location

    Target.value

    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

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    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
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    Last edited by SamT; 05-09-2017 at 06:12 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •