Excel macro - delete rows

bart1975

New Member
Joined
Apr 20, 2011
Messages
3
I need an excel macro to delete certain rows in a spreadsheet based on one column of the spreadsheet. If there is a "comma" in the name field i want the row deleted. I want a pause built in so I can refuse the delete before it happens. Any help on this would be much appreciated.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Do you want a "pause" before the deletion of each row or a one-time "pause" before all deletions are done ?
 
Upvote 0
items in 'red' below you will have to change depending on what column your field name resides in (in the below example it is in Column A):

Rich (BB code):
Sub DeleteRows()
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Dim i As Long
Dim s As Integer
For i = LastRow To 2 Step -1
    s = InStr(1, Range("A" & i), ",")
    
    If s > 0 Then
        
        Rows(i).Interior.ColorIndex = 36
        
        r = MsgBox("Delete highlighted row?", vbYesNo, "Delete Rows Prompt")
            
        If r = 6 Then
               Rows(i).Delete
            Else
               Rows(i).Interior.ColorIndex = 0
        End If
                
    End If
Next i
End Sub
 
Upvote 0
Thank you, that macro works very well, the only adjustment to it would be to page up as you delete rows. The page stays where you deleted the first row so you cant see the rows being deleted after the first page. I hope I'm making sense.
 
Upvote 0
see below modification

Code:
Sub DeleteRows()
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Dim i As Long
Dim s As Integer
For i = LastRow To 2 Step -1
    s = InStr(1, Range("A" & i), ",")
    
    If s > 0 Then
        
        With Rows(i)
            .Interior.ColorIndex = 36
            .Activate
        End With
        
        r = MsgBox("Delete highlighted row?", vbYesNo, "Delete Rows Prompt")
            
        If r = 6 Then
               Rows(i).Delete
            Else
               Rows(i).Interior.ColorIndex = 0
        End If
                
    End If
Next i
End Sub
 
Upvote 0
Thank you, that works great. One of these days I'm going to learn how to write some code. You're good and very fast!
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,728
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top