Macro To Delete All Rows When The Last Letter In Column A is An N

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
Is there a Macro To Delete All Rows When The Last Letter In Column A is An N please.

What would be beneficial is a code with an input box so I can put the letter as I will use it on all different occasions.

Thank.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hello, this should do as you ask:

Code:
Sub DeleteLetter()
    Dim sLet As String 'user defined letter to search for in column A
    Dim l As Long 'counter
    
    Dim fr As Long, lr As Long 'first row, last row
    
    fr = 2 'assumption here, change as necessary
    lr = Range("A" & Rows.Count).End(xlUp).Row 'last used row in col a
    
TryAgain:
    sLet = Application.InputBox("Please select letter to delete", "Select")
    If sLet = "False" Then Exit Sub
    
    If Len(sLet) <> 1 Then
        MsgBox "Please only select a single letter", vbCritical, "Incorrect input"
        GoTo TryAgain
    End If
    
    'if we get here, user has selected a letter
    
    'count from bottom up to delete rows:
    For l = lr To fr Step -1
       If Right(Range("A" & l), 1) = sLet Then Range("A" & l).EntireRow.Delete'is last letter same as user input?
    Next l
    
End Sub
 
Last edited:
Upvote 0
Thanks gallen seems to work a treat. I had to add the application screenupdating bit as I had some very big files.
 
Upvote 0
Yes, also, if you have a 'Worksheet_Change' event you will need to 'Application.EnableEvents = False' too to stop it it triggering on every deletion
 
Upvote 0

Forum statistics

Threads
1,215,129
Messages
6,123,218
Members
449,091
Latest member
jeremy_bp001

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