+ Reply to Thread
Results 1 to 5 of 5

Thread: Macro to delete entire rows where O2:O does not equal a date

  1. #1
    Valued Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    425

    Macro to delete entire rows where O2:O does not equal a date

    Hi
    Can someone suggest to me a good efficient macro that would delete rows where O2:O does not equal a date. So if I have this information in the column O as below:

    HEADER
    Complete
    Complete
    Date in past
    03/05/2011
    Complete
    Complete
    No info
    Quarrantined
    Complete
    Complete
    Complete
    Complete
    Complete
    18/04/2011
    18/04/2011
    Complete
    Complete
    Complete
    Then after the macro run it would only leave me header row and the rows with dates like this.
    HEADER
    03/05/2011
    18/04/2011
    18/04/2011
    Any ideas?


    Cheers
    Rain
    Last edited by rain4u; 04-20-2011 at 10:38 PM. Reason: Solved

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010
    Posts
    856

    Re: Macro to delete entire rows where O2:O does not equal a date

    Sub DelRows()
    Dim lngLastRow As Long
    lngLastRow = Range("O65536").End(xlUp).Row
    
    
    For Each c In Range("O2:O" & lngLastRow)
        If Not IsDate(c.Value) Then c.EntireRow.Delete
    Next
    
    End Sub

  3. #3
    Valued Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    425

    Re: Macro to delete entire rows where O2:O does not equal a date

    Hi
    Thank you for answering.
    Offered code does not remove all unwanted rows. Please see attached xls


    Any ideas
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor nimrod's Avatar
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10
    Posts
    609

    Re: Macro to delete entire rows where O2:O does not equal a date

    Offered code does not remove all unwanted rows.
    If you are going to delete by looping thru rows you MUST loop from the bottom and move up OR... you can delete all rows of certain type at one time like the following code does.
    This code deletes all rows that have a constant that is not a number or date.

    FAST ONE STEP METHOD
    Public Sub DeleteStringsLeaveDates()
       Range("O2:O" & Rows.Count).SpecialCells(xlCellTypeConstants, 22).EntireRow.Delete
    End Sub
    .
    .
    .
    .
    LOOP METHOD (slower but works)


    Public Sub LoopAndDelete()
       Const TargCol As String = "O"
          Application.ScreenUpdating = False
          BottomRow = Cells(Rows.Count, TargCol).End(xlUp).Row
       
          For iRow = BottomRow To 2 Step -1
             If Not IsDate(Cells(iRow, TargCol).Value) Then Cells(iRow, TargCol).EntireRow.Delete
          Next iRow
          
           Application.ScreenUpdating = True
    End Sub
    Last edited by nimrod; 04-20-2011 at 08:16 PM.
    Contactus ut Sentio

    YOUR FEEDBACK: To Say Thanks, or to leave Constructive Comments, please click on the Scales of Justice Icon at top of current post.

    >Develope Good Habits with MSDN Coding Standards <>How To Add Macros & VBA Code To Your Workbooks<>Best Practices For Referencing Cells, Ranges and Sheets<

  5. #5
    Valued Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    425

    Re: Macro to delete entire rows where O2:O does not equal a date

    The fast version will do. Thank you. Column O will always have either a date or text in it. It will never contain just numbers like "1" or "2". And even if it will have a number as the first character lets say number 2, it would always have some text to go along with it, so it would be classed as a text. Lets say "2 will be shipped". So the fast code fits the bill!
    Thank you!


    Cheers
    Rain

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0