Delete Row based only on 1st character of cell

canam

Board Regular
Joined
Dec 14, 2004
Messages
156
I have found the following code that does almost what I want. It deletes a row if a certain character is found in the row. What I need is the row to be deleted if specific character appears first in that row. For example if "C" is the first character in row 23 than that row will be deleted, but if "C" is the 10th character in row 24 it will not be delected. Any help is appreciated. Thanks

Code:
Option Compare Text
Sub DeleteRows()
Dim LstRw As Long, Rw As Long
LstRw = Cells(Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
For Rw = LstRw To 1 Step -1
  If InStr(Cells(Rw, "A"), "F") Or InStr(Cells(Rw, "A"), "C") Then _
    Rows(Rw).EntireRow.Delete
Next Rw
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi

Autofilter would be quicker than looping, significantly so if you have many criteria you want deleted. Something like:

Code:
Sub Del()
With Columns("A")
    .AutoFilter field:=1, Criteria1:="C*" 'ie begins with 'C'
    .Resize(Rows.Count - 1).Offset(1).EntireRow.Delete
End With
ActiveSheet.AutoFilterMode = False
End Sub
 
Upvote 0
Just change the value of Criteria = "g" to whatever character you want. It is case sensitive.

Code:
Sub Delete_Row_IF_First_Char_is()
Dim c As Long, Limit As Long, FirstChar As String, Criteria As String

Limit = Cells(Rows.count, 1).End(xlUp).Row
Criteria = "g"
For c = 1 To Limit
    FirstChar = Mid(Cells(c, 1), 1, 1)
    If FirstChar = Criteria Then
    Cells(c, 1).EntireRow.Delete
    End If
Next c
   
End Sub
 
Upvote 0
Thanks you both for the help.

Richard I am using your solution. It is very fast and does exactly what I want.

Wisewood your code didn't do anything when run. Thanks anyway.
 
Upvote 0
The code is case sensitive. Based on the above code, any rows where "g" was the first character should be deleted. They certainly were in my tests.
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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