c7c4c7
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
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
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
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
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
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
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
Delete-Row.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for all of the help it worled perfectly
Suggested revision:
Open in new window
Dave