+ Reply to Thread
Results 1 to 6 of 6

Delete Entire Row if cell contains specified value

Hybrid View

  1. #1
    Registered User
    Join Date
    01-28-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    17

    Delete Entire Row if cell contains specified value

    Hi, I'm very new to VBA and have come up against a situation that is beyond me and my research through the forums.

    I need to keep everything in rows 1,2 and 3.
    After that, I need to delete all the rows up until the first time a cell in column D contains a percentage and shift all the rows after that up with it. The amount of rows that will need to be deleted changes everyday that I update.

    So when the macro is completed, Row 4 should now be a row that contains a percentage in column D.

    I have attached a sample of the project. In this sample, Row 28 should end up being Row 4 after the macro runs.

    Thanks
    Peter
    Attached Files Attached Files
    Last edited by kosti; 01-05-2015 at 10:26 AM.

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Delete Entire Row if cell contains specified value

    Hi kosti,

    Thanks for being my 2,000th posting!!

    Try this (initially on a copy of your data as the results cannot be undone if they're not as expected):

    Option Explicit
    Sub Macro1()
        
        Dim lngMyCol As Long, _
            lngMyRow As Long
        Dim xlnCalcMethod As XlCalculation
                
        With Application
            xlnCalcMethod = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
    
        lngMyCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
        lngMyRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        With Columns(lngMyCol)
            With Range(Cells(4, lngMyCol), Cells(lngMyRow, lngMyCol))
                .Formula = "=IF(LEFT(CELL(""format"",D4),1)=""P"","""",NA())" 'http://www.ozgrid.com/forum/showthread.php?t=80506
                ActiveSheet.Calculate
                .Value = .Value
            End With
            On Error Resume Next 'Turn error reporting off - OK to ignore 'No cells found' message
                .SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
            On Error GoTo 0 'Turn error reporting back on
            .Delete
        End With
        
        With Application
            .Calculation = xlnCalcMethod
            .ScreenUpdating = True
        End With
        
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Delete Entire Row if cell contains specified value

    if i am understanding the question properly

    ...not sure if my logic is correct for all situations you are going to face but it works with example provided
    Attached Files Attached Files
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Delete Entire Row if cell contains specified value

    try this

    Sub abc()
     With Application
        .ScreenUpdating = False
        For i = Cells(Rows.Count, "e").End(xlUp).Row To 5 Step -1
           If Not Cells(i, "e").NumberFormat = "0%" Then
               Cells(i, "e").EntireRow.Delete
           End If
        Next
        .ScreenUpdating = True
     End With
    End Sub
    Last edited by mike7952; 01-05-2015 at 12:43 AM.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,633

    Re: Delete Entire Row if cell contains specified value

    This should do
    Sub test()
        Dim r As Range
        Set r = Cells.Find("%", , , 2)
        If Not r Is Nothing Then
            If r.Row > 4 Then Rows("4:" & r.Row - 1).Delete
        End If
    End Sub

  6. #6
    Registered User
    Join Date
    01-28-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Delete Entire Row if cell contains specified value

    Wow thank you to all the reply's. Worked like a charm.
    Last edited by kosti; 01-05-2015 at 10:24 AM.

+ 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. if cell is blank delete entire row
    By thuddleston11 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-26-2013, 03:38 PM
  2. delete entire row if cell value below X
    By Armitage2k in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-18-2013, 04:42 PM
  3. [SOLVED] Delete cell only with certain critria. Don't want to delete entire row, the celsl only
    By RobertOHare in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-16-2013, 04:18 AM
  4. IF cell contains value - delete entire row
    By Chris2010 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-30-2010, 10:44 PM
  5. Delete entire row when cell says delete
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-27-2007, 11:46 AM

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