Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Code to delete entire row deletes entire named range instead

Posted on 2012-03-28
8
Medium Priority
?
304 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

916 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