+ Reply to Thread
Results 1 to 3 of 3

Worksheet Change Code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Question Worksheet Change Code

    Hi all,

    I want to use the code below to analyze a spreadsheet and calculat the amounts still outstanding from column R. I have added two columns, AA and AB. If the amount in column R is still outstanding, the user enters N (for not collected) in column AA and the outstanding amount from column R would be copied to column AB. When the spreadsheet is analyzed at a later date and the outstanding amount was collected, the user deletes the N in column AA and the amount in column AB should be deleted also. However, my code is not deleting the amount. Any help?

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cRange As Range
    Set cRange = Range("AA5:AA" & Cells(Rows.Count, 1).End(xlUp).Row)
    If Target.Cells.Count > 1 _
    Or Intersect(Target, Range("AA5:AA" & Cells(Rows.Count, 1).End(xlUp).Row)) Is Nothing Then Exit Sub
            If UCase(Cells(Target.Row, "AA")) = "N" Then
    
                    Cells(Target.Row, "AB") = Cells(Target.Row, "R")
    
            If Cells(Target.Row, "AA") = vbNullString Then
                    Cells(Target.Row, "AB") = vbNullString
    
            End If
            End If
    
    End Sub
    Thank you,
    Gos-C
    Using Excel 2010 & Windows 10
    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

  2. #2
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: Worksheet Change Code

    It appears that your second IF statement is nested within the first IF statement. This means that the second IF statement will never occur because if the target cell is not "N", the second IF statement will not be run when it should.

    Try amending the code to be

    If UCase(Cells(Target.Row, "AA")) = "N" Then
    Cells(Target.Row, "AB") = Cells(Target.Row, "R")
    ElseIf Cells(Target.Row, "AA") = vbNullString Then
    Cells(Target.Row, "AB") = vbNullString
    End If
    Hope this helps.

  3. #3
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Thumbs up Re: Worksheet Change Code

    That worked!

    Thank you, quekbc.

    Cheers,
    Gos-C

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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