+ Reply to Thread
Results 1 to 4 of 4

Code Issues: Throws error everytime I add a row

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Code Issues: Throws error everytime I add a row

    I have used to following code to self adjust cells within a WS based upon the output in others. The issue is that when I insert a new "Blank" row I get an error code everytime. I understand why I just don't understand how to fix it any suggestions would be appreciated:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("M2:M" & Range("M" & Rows.count).End(3)(1).Row)) Is Nothing Then
        Select Case Target.Value
            Case Is = "Full Compliance"
                Cells(Target.Row, "S").Value = "D"
                Cells(Target.Row, "G").Value = Cells(Target.Row, "G").Value + 1095
            Case Is = "Partial Compliance"
                Cells(Target.Row, "S").Value = "C"
                Cells(Target.Row, "G").Value = Cells(Target.Row, "G").Value + 365
            Case Is = "Non-Compliance[Absent]"
                Cells(Target.Row, "S").Value = "A"
                Cells(Target.Row, "G").Value = Cells(Target.Row, "G").Value + 30
            Case Is = "Non-Compliance[Imminent Danger]"
                Cells(Target.Row, "S").Value = "B"
                Cells(Target.Row, "G").Value = Cells(Target.Row, "G").Value + 180
        End Select
    End If
    End Sub

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Code Issues: Throws error everytime I add a row

    Hi,
    Try this :

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("M2:M" & Range("M" & Rows.Count).End(3)(1).Row)) Is Nothing Then
        Select Case Target.Value
            Case Is = "Full Compliance"
                Cells(Target.Row, "S").Value = "D"
                Cells(Target.Row, "G").Value = Cells(Target.Row, "G").Value + 1095
            Case Is = "Partial Compliance"
                Cells(Target.Row, "S").Value = "C"
                Cells(Target.Row, "G").Value = Cells(Target.Row, "G").Value + 365
            Case Is = "Non-Compliance[Absent]"
                Cells(Target.Row, "S").Value = "A"
                Cells(Target.Row, "G").Value = Cells(Target.Row, "G").Value + 30
            Case Is = "Non-Compliance[Imminent Danger]"
                Cells(Target.Row, "S").Value = "B"
                Cells(Target.Row, "G").Value = Cells(Target.Row, "G").Value + 180
        End Select
    End If
    End Sub
    Trick is to perform the code only if ONE cell is changed so if Target.Count = 1...
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Code Issues: Throws error everytime I add a row

    Quote Originally Posted by GC Excel View Post
    Hi,
    Try this :

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("M2:M" & Range("M" & Rows.Count).End(3)(1).Row)) Is Nothing Then
        Select Case Target.Value
            Case Is = "Full Compliance"
                Cells(Target.Row, "S").Value = "D"
                Cells(Target.Row, "G").Value = Cells(Target.Row, "G").Value + 1095
            Case Is = "Partial Compliance"
                Cells(Target.Row, "S").Value = "C"
                Cells(Target.Row, "G").Value = Cells(Target.Row, "G").Value + 365
            Case Is = "Non-Compliance[Absent]"
                Cells(Target.Row, "S").Value = "A"
                Cells(Target.Row, "G").Value = Cells(Target.Row, "G").Value + 30
            Case Is = "Non-Compliance[Imminent Danger]"
                Cells(Target.Row, "S").Value = "B"
                Cells(Target.Row, "G").Value = Cells(Target.Row, "G").Value + 180
        End Select
    End If
    End Sub
    Trick is to perform the code only if ONE cell is changed so if Target.Count = 1...
    GC seems to work can you maybe explain what the issue was just for my knowledge.

    Thanks for the help

  4. #4
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Code Issues: Throws error everytime I add a row

    When you insert a new row, the Target address is the full row (example $15:$15), not a single cell.
    Therefore, you can not use the Select Case to verify the value.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. 2007 VBA Code in 2010 Throws Multiple Errors: Runtime Error 91 and more
    By VB@N3wb|3 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-23-2013, 09:54 AM
  2. Simple If statement throws an error
    By ronanm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-01-2011, 10:07 AM
  3. workbook.open throws 1004 error
    By gary.smith@primeexalia.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-03-2006, 06:45 PM
  4. How do I perform a certain function if VBA throws up an error?
    By Matt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-24-2006, 11:10 AM
  5. [SOLVED] [Q] Save As throws type mismatch error in control's code?
    By Jason Weiss in forum Excel General
    Replies: 1
    Last Post: 07-16-2005, 12:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1