+ Reply to Thread
Results 1 to 7 of 7

Would like to delete rows based on cell content

Hybrid View

  1. #1
    Registered User
    Join Date
    08-15-2012
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    73

    Would like to delete rows based on cell content

    Good Morning All,

    The attached sample spreadsheet is divided into two sections. The top section is regular sales orders and the bottom section is non regular sales orders. I'm currently using the code below to delete rows based on color of column A. I would like to modify the current code to delete rows based on column A for regular sales orders only and to delete non regular sales orders based on SO status in column Q. Any non regular sales order with a SO status in column Q of A or S should be deleted with the macro. Any help is appreciated.

    Sub deleteRowOnCollor()
        Dim lLR, lC As Long
        Application.ScreenUpdating = False
        lLR = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row 'Determine last row
        For lC = lLR To 3 Step -1
            Sheets(1).Cells(lC, 1).Activate
            If ActiveCell.Interior.Color = 15773696 Then 'Used index color.
                Rows(lC).Delete Shift:=xlUp 'delete row
            End If
        Next
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Would like to delete rows based on cell content

    Sub delete_non_reg_sales_orders()
        Dim lLR As Long, lC As Long
        Application.ScreenUpdating = False
        With Sheets(1)
            lLR = .Range("A" & Rows.Count).End(xlUp).Row 'Determine last row
            For lC = lLR To 3 Step -1
                If UCase(.Range("Q" & lC).Value) = "A" Or UCase(.Range("Q" & lC).Value) = "S" Then
                    .Rows(lC).Delete Shift:=xlUp 'delete row
                End If
            Next lC
        End With
        Application.ScreenUpdating = True
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    08-15-2012
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    73

    Re: Would like to delete rows based on cell content

    AlphaFrog,

    Thanks for the quick response. There is just one issue. I only want to delete rows in the bottom half of the sheet based on content in column Q. Your code deletes all rows based on column Q.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Would like to delete rows based on cell content

    Quote Originally Posted by cindywylie View Post
    AlphaFrog,

    Thanks for the quick response. There is just one issue. I only want to delete rows in the bottom half of the sheet based on content in column Q. Your code deletes all rows based on column Q.
    This only deletes between the two "Non-Regular Sales Orders" headers in column A

    Sub delete_non_reg_sales_orders()
        Dim lLR As Long, lC As Long, nrso As Integer
        Application.ScreenUpdating = False
        With Sheets(1)
            lLR = .Range("A" & Rows.Count).End(xlUp).Row 'Determine last row
            For lC = lLR To 3 Step -1
                If .Range("A" & lC).Value = "Non-Regular Sales Orders" Then nrso = nrso + 1
                If nrso = 1 Then
                    If UCase(.Range("Q" & lC).Value) = "A" Or UCase(.Range("Q" & lC).Value) = "S" Then
                        .Rows(lC).Delete Shift:=xlUp 'delete row
                    End If
                ElseIf nrso > 1 Then
                    Exit For
                End If
            Next lC
        End With
        Application.ScreenUpdating = True
    End Sub
    Last edited by AlphaFrog; 02-27-2014 at 12:15 PM.

  5. #5
    Registered User
    Join Date
    08-15-2012
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    73

    Re: Would like to delete rows based on cell content

    AlphaFrog,

    Perfect! Is there anyway to combine the macro for the top half and the macro you created. If I could have one macro for this sheet, that would be great. If thats too much I understand. Thanks for your time.

    Sub deleteRowOnCollor()
        Dim lLR, lC As Long
        Application.ScreenUpdating = False
        lLR = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row 'Determine last row
        For lC = lLR To 3 Step -1
            Sheets(1).Cells(lC, 1).Activate
            If ActiveCell.Interior.Color = 15773696 Then 'Used index color.
                Rows(lC).Delete Shift:=xlUp 'delete row
            End If
        Next
        Application.ScreenUpdating = True
    End Sub

    Sub delete_non_reg_sales_orders()
        Dim lLR As Long, lC As Long, nrso As Integer
        Application.ScreenUpdating = False
        With Sheets(1)
            lLR = .Range("A" & Rows.Count).End(xlUp).Row 'Determine last row
            For lC = lLR To 3 Step -1
                If .Range("A" & lC).Value = "Non-Regular Sales Orders" Then nrso = nrso + 1
                If nrso = 2 Then Exit For
                If UCase(.Range("Q" & lC).Value) = "A" Or UCase(.Range("Q" & lC).Value) = "S" Then
                    .Rows(lC).Delete Shift:=xlUp 'delete row
                End If
            Next lC
        End With
        Application.ScreenUpdating = True
    End Sub

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Would like to delete rows based on cell content

    Sub delete_sales_orders()
        Dim lLR As Long, lC As Long, Section As String
        Application.ScreenUpdating = False
        With Sheets(1)
            lLR = .Range("A" & Rows.Count).End(xlUp).Row 'Determine last row
            For lC = lLR To 3 Step -1
                If .Range("A" & lC).Value Like "*Sales Orders" Then Section = .Range("A" & lC).Value
                Select Case Section
                    Case "Regular Sales Orders"
                        If .Range("A" & lC).Interior.Color = 15773696 Then 'Used index color.
                            .Rows(lC).Delete Shift:=xlUp 'delete row
                        End If
                    Case "Non-Regular Sales Orders"
                        If UCase(.Range("Q" & lC).Value) = "A" Or UCase(.Range("Q" & lC).Value) = "S" Then
                            .Rows(lC).Delete Shift:=xlUp 'delete row
                        End If
                End Select
            Next lC
        End With
        Application.ScreenUpdating = True
    End Sub

  7. #7
    Registered User
    Join Date
    08-15-2012
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    73

    Re: Would like to delete rows based on cell content

    Works perfectly. Thanks so much for your help!

+ 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. [SOLVED] Find rows to delete with a macro based on cell content
    By mkraffert in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-28-2013, 06:27 PM
  2. delete certain rows based on their partial cell content
    By WasWodge in forum Excel General
    Replies: 3
    Last Post: 01-30-2011, 02:37 PM
  3. Delete rows based on cell content
    By scaffdog845 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 05-06-2010, 07:34 PM
  4. Need help to delete rows based on cell content
    By iturnrocks in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-17-2008, 12:10 PM
  5. Replies: 2
    Last Post: 10-03-2005, 10:05 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