Deleting Words Based on Number of Words

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello Folks, I want to delete few words in A2 cell of all the sheets in my WB. Please go through the 4 strings and their expected results. I want to keep the last numeric value of the string and if the string contains Alphabet as last word it should keep both numeric value and alphabet also.The string always has minimum of 2 and maximum of 4 words. I tried with formulas it doesn't work for all the 4 scenarios at same time. It seems some mathematical coding is required. Anyone have solution or suggestion please heads up. Thank you.

Actual
Expected
21 C 220
220
21 C 220 B
220 B
21 220 B
220 B
21 220
220

<tbody>
</tbody>



<tbody>
</tbody>
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hello Istvan, It working great. If it is in vba format it will be good. Thank you.
 
Upvote 0
Can you please explain this part (1+(RIGHT(A2,1)>="A"))*50))

(RIGHT(a2,1)>="A") is TRUE (or 1) if the last character of the string is a letter, otherwise zero. So, in the former case 1+(right(a2,1)>="A") is 2, otherwise 1. It means that we go back from the right 2*50, or 1*50 characters, that involves the last two, or the last one substring, resp.
 
Upvote 0
Here is a UDF that can be used in VBA or as a worksheet function.
Code:
Function LastNumeralPlus(aString As String)
    Dim arrNum(0 To 9) As Long
    Dim i As Long
    For i = 0 To 9
        arrNum(i) = InStrRev(aString, " " & i)
    Next i
    LastNumeralPlus = Mid(aString, WorksheetFunction.Max(arrNum) + 1)
End Function
 
Upvote 0
Here is a UDF that can be used in VBA or as a worksheet function.
Code:
Function LastNumeralPlus(aString As String)
    Dim arrNum(0 To 9) As Long
    Dim i As Long
    For i = 0 To 9
        arrNum(i) = InStrRev(aString, " " & i)
    Next i
    LastNumeralPlus = Mid(aString, WorksheetFunction.Max(arrNum) + 1)
End Function

Hello Mike, UDF is working great. But if my numerical value has -(minus) symbol along with alphabet it is not working. If numerical value has -(minus) symbol without alphabet it is working. Any solution for that?
 
Last edited:
Upvote 0
Could you give a couple of examples of the strings that are giving you problems?

Are the "-" always associated with a number? Might "ABC -20 cat 3 -xyz" be an input string.

You might try

Code:
Function LastNumeralPlus(aString As String)
    Dim arrNum(0 To 10) As Long
    Dim i As Long
    For i = 0 To 9
        arrNum(i) = InStrRev(aString, " " & i)
    Next i
    arrNum(10) = InStrRev(aString, " -")
    LastNumeralPlus = Mid(aString, WorksheetFunction.Max(arrNum) + 1)
End Function
 
Upvote 0
Or
Code:
Function LastNumeralPlus(aString As String)
    Dim arrNum(0 To 9) As Long
    Dim i As Long
    For i = 0 To 9
        arrNum(i) = WorksheetFunction.Max(InStrRev(aString, " " & i), InStrRev(aString, " -" & i))
    Next i
    LastNumeralPlus = Mid(aString, WorksheetFunction.Max(arrNum) + 1)
End Function
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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