+ Reply to Thread
Results 1 to 5 of 5

Row Deletion w/ VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    02-05-2016
    Location
    pennsylvania
    MS-Off Ver
    2011
    Posts
    10

    Row Deletion w/ VBA

    I am trying to write a VBA that deletes the Row if column U = "Delivered". Column U is an index/match function pulling from another workbook and I've added the text function but I still keep running into the same error of no match. Any additional conversions I put into the VBA really slows down the running of it because it is going from line to line and there are like 40,000 lines. Is there a better way I could be doing this.Here is that code below and a sample file attached.

    Sub deleteRows()

    Dim lastRow As Long
    Dim i As Long

    lastRow = Cells(Rows.Count, "U").End(xlUp).Row

    For i = lastRow To 1 Step -1
    If Cells(i, "U").Value = "Delivered" Then
    Rows(i).Delete
    End If
    Next i

    End Sub
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,190

    Re: Row Deletion w/ VBA

    how about:

    Dim lastRow, i
    
    On Error Resume Next
    
    lastRow = ActiveSheet.UsedRange.Rows.Count
    Range("U" & lastRow).Select
    
    For i = lastRow To 1 Step -1
       If ActiveCell.Value = "Delivered" Then Rows(i).Delete
       ActiveCell.Offset(-1, 0).Select
    Next
    MsgBox "Done"

  3. #3
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,942

    Re: Row Deletion w/ VBA

    Try this, without the need to loop through row by row.
    Sub DelDeliveredRows()
        Application.DisplayAlerts = False
        With ThisWorkbook.Sheets("Sheet1").ListObjects(1)
             .Range.AutoFilter Field:=21, Criteria1:="Delivered"
             .DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
             .Range.AutoFilter
        End With
        Application.DisplayAlerts = True
    End Sub
    Or this if there's a chance no rows will be "Delivered"
    Sub DelDeliveredRows()
        Application.DisplayAlerts = False
        With ThisWorkbook.Sheets("Sheet1").ListObjects(1)
             .Range.AutoFilter Field:=21, Criteria1:="Delivered"
             On Error Resume Next
             .DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
             On Error GoTo 0
             .Range.AutoFilter
        End With
        Application.DisplayAlerts = True
    End Sub
    BSB
    Last edited by BadlySpelledBuoy; 04-10-2023 at 03:53 PM.

  4. #4
    Registered User
    Join Date
    02-05-2016
    Location
    pennsylvania
    MS-Off Ver
    2011
    Posts
    10

    Re: Row Deletion w/ VBA

    The first one seemed to work. I'll keep testing it but thank you!

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Row Deletion w/ VBA

    Another Option...
    Sub J3v16()
    With ActiveSheet.ListObjects(1).DataBodyRange.Columns(21)
        .Value = Evaluate("=If(Row(" & .Address & "),If(" & .Address & "=""Delivered"",""""," & .Address & "))")
        .SpecialCells(xlCellTypeBlanks).Delete xlShiftUp
    End With
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Duplicate deletion macro does not match number of rows for deletion by hand
    By Ianmacros in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-04-2019, 06:07 PM
  2. Prevent workbook deletion or create the copy upon the deletion?
    By ciapul12 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-29-2014, 04:35 PM
  3. #REF after row deletion
    By ScottyHez in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-17-2014, 05:05 AM
  4. row deletion
    By jusciusr in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-19-2013, 10:08 AM
  5. Row deletion
    By mtfd in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-13-2011, 11:15 AM
  6. No Deletion!
    By Peanut_M in forum Excel General
    Replies: 2
    Last Post: 01-11-2011, 05:31 AM
  7. Row Deletion
    By ringrim in forum Excel General
    Replies: 1
    Last Post: 08-20-2008, 05:21 PM

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