Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Code to delete entire row deletes entire named range instead

Posted on 2012-03-28
8
Medium Priority
?
302 Views
Last Modified: 2012-03-28
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
0
Comment
Question by:c7c4c7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 37778343
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
0
 

Author Comment

by:c7c4c7
ID: 37778526
rdelete is always = Nothing, does it have to be set to the named range
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37778567
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

0
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

Author Comment

by:c7c4c7
ID: 37778724
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
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37778760
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
0
 

Author Comment

by:c7c4c7
ID: 37779091
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
0
 
LVL 42

Accepted Solution

by:
dlmille earned 1000 total points
ID: 37779380
I'm not sure, but let's be explicit (my preferred approach):

  Set wsMaster = Sheets("Master")

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

    End If
    Next wscell

MsgBox "The name of the active sheet is " & ActiveSheet.Name
msgbox "Going to delete from" & rdelete.worksheet.name

    rDelete.EntireRow.Delete

Open in new window


PS - I almost NEVER anymore reference a sheet without a workbook prefix, or a range without a worksheet prefix.  It keeps me out of trouble.

Let me know if this helped.

Dave
0
 

Author Closing Comment

by:c7c4c7
ID: 37780146
Thanks for all of the help it worled perfectly
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question