Consulting

Results 1 to 4 of 4

Thread: Solved: need to contain clearing of row within a range

  1. #1
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location

    Solved: need to contain clearing of row within a range

    Here is what I have:
    Private Sub Worksheet_Change(ByVal Target As Range)
     'this is to select adjacent cell after typing a quantity
     If Not Intersect(Target, Range("A17:A35")) Is Nothing Then Target.Offset(0, 1).Select
     
    'this in case user selects a duplicate item & gives the user the choice of adding same to the previous row or delete it
    Dim rngFindRange As Range
    Application.EnableEvents = False
    If Intersect(Target, Range("A:B")) Is Nothing Or Me.UsedRange.Rows.Count = 1 Then GoTo TidyUp
    If Target.Count > 1 Then
        MsgBox "Please update only one cell at a time in columns A & B.", vbInformation
        Application.Undo
        GoTo TidyUp
     
    End If
    If Range("A" & Target.Row) <> "" And Range("B" & Target.Row) <> "" Then
        Set rngFindRange = Range("B1:B" & Target.Row - 1).Find(What:=Range("B" & Target.Row), LookIn:=xlValues)
            If Not rngFindRange Is Nothing Then
     
                If MsgBox("Product exists on a previous row ... Add to it?", vbYesNo) = vbNo Then
                    Target.EntireRow.ClearContents
                    GoTo TidyUp
                End If
     
                rngFindRange.Offset(0, -1) = rngFindRange.Offset(0, -1) + Range("A" & Target.Row)
     
                Target.EntireRow.ClearContents
                 End If
                End If
     
     
                Dim LastRow As Long
        Range("A35").Select
        LastRow = ActiveCell.End(xlUp).Offset(1, 0).Select
        Selection.EntireRow.ClearContents
     Application.EnableEvents = True
     
    TidyUp:
     
    End Sub
    example:
    I type qty 5 / & choose from the userform a product of my choice.
    Assuming that I have already done this choice in a row above that one I get prompted to either add to the previous row or cancel.

    In any decision the row I am typing in will be "cleared"

    The problem is that in both instances the code clears the whole row instead of the row within columns [A:D] as it should.:D
    The range where the edit takes place needs to be contained within [A17:E35]

    P.S I am not responsible for the smiley that appears in my message.Underneath the first smiley there is the letter D
    Attached Files Attached Files
    Thank you for your help

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    This is the line that is clearing the whole row[vba]Target.EntireRow.ClearContents
    [/vba]change it to[vba]Range("A" & Target.row & ":E" & Target.Row).ClearContents[/vba]You also have [vba]Selection.EntireRow.ClearContents
    [/vba]If you still want to keep within your parameters change this too
    (i am responsible for the smiley!, you can turn those off when clicking "Quote" and replying you get the option prior to submiting, i've disabled the smilies for you)
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location
    Works great now.
    Thank you again Simon, It is 11:18PM over here and time for bed.
    Thank you for your help

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    lol it's 04:46am here!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

Posting Permissions

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