Link to home
Start Free TrialLog in
Avatar of c7c4c7
c7c4c7Flag for United States of America

asked on

Code to delete entire row deletes entire named range instead

See the attached code

Worksheet, Master, is created by copying Sheet1 to it.  I have a named range that I search and when I find the word no in it I want to delete that entire row.  However when I attempt the delete all the rows in the named range are deleted instead.

What did I do wrong?

Thanks
Delete-Row.txt
Avatar of dlmille
dlmille
Flag of United States of America image

In your loop, you delete all the rows based on Selection, which is unrelated to the macro.  You want to use wsCell, rather than selection.

Suggested revision:

    Dim rDelete As Range
    For Each wscell In Range("yesNo")
    If wscell.Value = "no" Then
    MsgBox "no"
        If Not rDelete Is Nothing Then
            Set rDelete = wscell
        Else
            Set rDelete = Union(rDelete, wscell)
        End If
    End If
    Next wscell

    rdelete.EntireRow.delete

Open in new window


Dave
Avatar of c7c4c7

ASKER

rdelete is always = Nothing, does it have to be set to the named range
Sorry - type-o on my part:

    Dim rDelete As Range
    For Each wscell In Range("yesNo")
    If wscell.Value = "no" Then
    MsgBox "no"
        If  rDelete Is Nothing Then
            Set rDelete = wscell
        Else
            Set rDelete = Union(rDelete, wscell)
        End If
    End If
    Next wscell

    rdelete.EntireRow.delete

Open in new window

Avatar of c7c4c7

ASKER

Still not deleting anything, I noticed that rDelete.EntireRow.Delete was outside of the for loop so I changed it like this

     Set rDelete = Union(rDelete, wscell)
        End If
    rdelete.EntireRow.delete

Thinking that was the problem but even then no rows get deleted even though the entire column has nothing but "no" in the entire named range
See attached demonstration workbook using that code.

Perhaps you have a case sensitivity problem?  

Perhaps "no" is really "NO" or "No" or "nO"???

If that's the case, change:

If wsCell.Value = "no" Then


to

If UCASE(wsCell.Value) = "NO" then

See attached using the original code.

If that's not it, you're going to need to upload some of your data/example sheet so we can quickly diagnose.

Dave
deleteYesNoRoutine-r1.xls
Avatar of c7c4c7

ASKER

The reason I wasn't seeing the changes/deletions was because it was deleting from Sheet1 rather than Master that I had previously set as the active sheet.  Why is that?  I've attached a fresh copy of the code
Delete-Row.txt
ASKER CERTIFIED SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of c7c4c7

ASKER

Thanks for all of the help it worled perfectly