Deleting Last Word in the Cell based on Condition

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello all, i want to delete the last word in the cell if it is not equal "OK". My data is in A column and range is dynamic. The macro has look into all the cells in the column and delete the last word, if it is not equal to "OK". Thank you.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this with a copy of your workbook.

VBA Code:
Sub Delete_Last_Word()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(#="""","""",if(right(#,3)="" OK"",#,trim(left(#,len(#)-len(trim(right(substitute(#,"" "",rept("" "",50)),50)))))))", "#", .Address))
  End With
End Sub
 
Upvote 0
Solution
A loop should do it if the data range is not too large
Code:
Sub With_A_Loop()
Dim c As Range
    For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        If Mid(c, InStrRev(c, " ") + 1, 99) <> "OK" Then c.Value = Left(c, InStrRev(c, " ") - 1)
    Next c
End Sub

Peter is way ahead of me, both in time and excel intelligence with that code.
 
Upvote 0
A loop should do it if the data range is not too large
Just a couple of comments
  1. Your code will error if there are any blank cells or cells with only one word.
  2. A shorter way of testing
    If Mid(c, InStrRev(c, " ") + 1, 99) <> "OK"
    would be
    If Right(c, 3) <> " OK"
 
Upvote 0
You're right. Too much in a hurry.
Thanks Peter.
 
Upvote 0
Try this with a copy of your workbook.

VBA Code:
Sub Delete_Last_Word()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(#="""","""",if(right(#,3)="" OK"",#,trim(left(#,len(#)-len(trim(right(substitute(#,"" "",rept("" "",50)),50)))))))", "#", .Address))
  End With
End Sub
Thanks Peter, it is working fine. Thanks for your time.
 
Upvote 0

Forum statistics

Threads
1,216,104
Messages
6,128,856
Members
449,472
Latest member
ebc9

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