Solved

Code to delete entire row deletes entire named range instead

Posted on 2012-03-28
8
300 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
 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

 

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 250 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

615 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