+ Reply to Thread
Results 1 to 11 of 11

Delete method of Range class failed (again)

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2011
    Location
    Des Moines, Iowa, USA
    MS-Off Ver
    Excel 2007
    Posts
    2

    Delete method of Range class failed (again)

    When I run the following code, I get a Run-time Error '1004': Delete method of Range class failed. If there is nothing in column D, then the if statement is not satisfied and the macro runs fine and doesn't do anything.
    Changing the Delete line to 'Rows(i).Delete Shift=x1Up' works just fine, but I would prefer to just delete the selected range.
    Here is the entire macro:
    Sub AssignRequests()
        Dim i As Integer
        i = 2
        Do While i <= Range("OutstandingCount") + 10
        If Range("D" & i) <> "" Then
        Range("PasteRange").Value = Range("A" & i, "D" & i).Value
        
           Range("A" & i, "D" & i).Delete Shift:=x1Up
        
        Else: i = i + 1
        End If
        Loop
    End Sub
    This is my first post, so I hope it is done correctly.
    Thanks!
    Attached Files Attached Files
    Last edited by Kelden; 06-20-2011 at 10:45 AM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Delete method of Range class failed (again)

    Hi Kelden, you have to delete the entire row. Replace
    Range("A" & i, "D" & i).Delete Shift:=x1Up
    with
    Range("A" & i, "D" & i).EntireRow.Delete Shift:=x1Up
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Delete method of Range class failed (again)

    Hi Kelden,

    You might also want to take a closer look at

    Shift:=x1Up
    That should be a xlUp not a 1
    HTH
    Regards, Jeff

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Delete method of Range class failed (again)

    It's not X1UP but XLUP.
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Delete method of Range class failed (again)

    Mordred,
    You do not have to delete entire rows, you can delete cells just as you do in the UI.

  6. #6
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Delete method of Range class failed (again)

    Good eyes guys! I didn't even notice that, I just typed in what I did and it worked, even with the x1Up. Quirky.

    @romperstomper, how then would you know when to use EntireRow or not?

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Delete method of Range class failed (again)

    If you delete the entire row, there's no way to go but up, so it gets ignored.

    It's up to you whether you want the whole row deleted or not. If you just want to take a few cells out of a table, you may just want to delete the cells so as not to disturb other columns on the sheet.

  8. #8
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Delete method of Range class failed (again)

    In that case, wouldn't
    .ClearContents
    be more appropriate? I feel like I am spamming this thread!

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Delete method of Range class failed (again)

    Only if you want empty cells in the middle of your table. If you delete the cells, then the cells below (or to the right of) them in the same columns (or rows) shift up (or left) to fill in the spaces. If you clearcontents, then nothing moves.

  10. #10
    Registered User
    Join Date
    06-17-2011
    Location
    Des Moines, Iowa, USA
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Delete method of Range class failed (again)

    Hi guys,
    Thanks for the quick replies! 'Shift:=xlUp' worked as I would like it to. Your help is much appreciated!!

  11. #11
    Registered User
    Join Date
    05-27-2011
    Location
    mysore
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Delete method of Range class failed (again)

    This Error occurs if your sheet is protected. Unprotect and delete it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1