PDA

View Full Version : Solved: Delete Cell Content based on criteria



Sarfaraz
02-07-2013, 11:58 PM
I have a work sheet and in column "B" exluding B1 there are entries like this
LSB5268
LSB5236+LSB5236
JP9281
LS5312
JA5231+JA5231

Need a code which find the cell with "+" sign and delete everything after "+" sign including itself. After the Macro the cells should look like this
LSB5268
LSB5236
LS5312
JA5231

Would be highly grateful if somebody can help me in righting the code.

Thanks

zagrijs
02-08-2013, 02:13 AM
The following procedure will do it. Please note that i have provided for only 5 rows as in your example. Change the 5 to whatever number of rows you need.
Public Sub deleteCellContentsBasedOnCriteria()
On Error Resume Next
For i = 1 To 5 'change 5 to the number of rows with text in your column b
Cells(i, 2).Value = Left(Cells(i, 2).Value, WorksheetFunction.Find("+", Cells(i, 2).Value, 1) - 1)
Next i

End Sub

Bob Phillips
02-08-2013, 02:59 AM
Public Sub ProcessData()
Dim Lastrow As Long
Dim pos As Long
Dim i As Long

Application.ScreenUpdating = False

With ActiveSheet

Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = Lastrow To 2 Step -1

pos = InStr(.Cells(i, "A").Value, "+")
If pos > 0 Then .Cells(i, "A").Value = Left$(.Cells(i, "A").Value, pos - 1)
Next i
End With

Application.ScreenUpdating = True
End Sub

Sarfaraz
02-08-2013, 03:15 AM
Dear zagrijs & xld
Thanks for the codes both worked perfectly fine. Thanks a lot again

zagrijs
02-08-2013, 06:22 AM
You are welcome. I see you are new here. Kindly go to your original psoting, click thread tools and click "solved". Else you might find people not willing to help you in future.

Bob Phillips
02-08-2013, 12:31 PM
Kindly go to your original psoting, click thread tools and click "solved". Else you might find people not willing to help you in future.

Really? you wouldn't help him because he has not marked previous threads as solved?

shrivallabha
02-10-2013, 09:14 AM
You can do this without loop:
Public Sub ReplaceAfterPlus()
Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row).Replace "+*", vbNullString
End Sub

zagrijs
02-10-2013, 11:32 PM
@xld. I didn't suggest that I wouldn't help him because he hasn't marked the treat "solved", I used the word "people". I was simply relating to him what I have seen other regulars and experts saying about it and tried to make him aware of it. If I sounded insensitive, I apologise.