Solved

Code to delete entire row deletes entire named range instead

Posted on 2012-03-28
8
294 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
  • 4
  • 4
8 Comments
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
Comment Utility
rdelete is always = Nothing, does it have to be set to the named range
0
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
 

Author Comment

by:c7c4c7
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
Comment Utility
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 41

Accepted Solution

by:
dlmille earned 250 total points
Comment Utility
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
Comment Utility
Thanks for all of the help it worled perfectly
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now