Removing specific characters from end of a text

dankar

Board Regular
Joined
Mar 23, 2016
Messages
113
Office Version
  1. 365
Platform
  1. Windows
Hi, i have a list of 15k row, i want to remove all the ones ending with "_number" like _1 , _5, _99

the number after the underscore could be from 1 to 99. I need the last underscore and any number after it to be removed.


1694188312998.png


The result should be like this for this example:

1694188475451.png


thank you
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
With formula:

varios 08sep2023.xlsm
AB
1
22060_05002060_0500
32060_0500_12060_0500
42060_0500_22060_0500
52060_0500_992060_0500
610152_0812210152_08122
710152_08122_110152_08122
810152_08122_210152_08122
Hoja1
Cell Formulas
RangeFormula
B2:B8B2=IF(LEN(A2)-LEN(SUBSTITUTE(A2,"_",""))=2,TRIM(LEFT(SUBSTITUTE(A2,"_",REPT(" ",99),2),99)),A2)



With macro:
VBA Code:
Sub Removing_specific_characters()
  With Range("A2", Range("A" & Rows.Count).End(3))
    .Value = Evaluate("=IF({1},IF(LEN(" & .Address & ")-LEN(SUBSTITUTE(" & .Address & ",""_"",""""))=2,TRIM(LEFT(SUBSTITUTE(" & .Address & ",""_"",REPT("" "",99),2),99))," & .Address & "))")
  End With
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
 
Last edited:
Upvote 0
Part of the actual list:

ID
SAM_19517_03052010
SAM_19517_10262016
SAM_19517_10262016_1
SAM_19517_10282016
SAM_19517_10282016_1
SAM_19517_10282016_2
SAM_19517_10282016_3
SAM_19517_10282016_4
SAM_19517_10282016_5
SAM_19517_10282016_6
SAM_19517_10282016_7
SAM_19517_10282016_8
SAM_19517_10282016_9
SAM_19517_10282016_10
BOB_21766_7052_09212009
BOB_21766_7052_09212009_1
BOB_21766_7052_09212009_2
BOB_21766_7052_09212009_3
BOB_21766_7052_09212009_4
BOB_21766_7052_09212009_5
BOB_21766_7052_09212009_6
BOB_21766_7052_09212009_7
BOB_21766_7052_09212009_8
BOB_21766_7052_09212009_9
BOB_21766_7052_09212009_10
BOB_21766_7052_09212009_11
BOB_21766_7052_09212009_12
BOB_21766_7052_09212009_13
BOB_21766_7052_09212009_14
BOB_21766_7052_09212009_15
BOB_21766_7052_09212009_16
BOB_21766_7052_09212009_17
BOB_21766_7052_09212009_18
BOB_21766_7052_09212009_19
BOB_21766_7052_09212009_20
BOB_21766_7052_09212009_21
BOB_21766_7052_09212009_22
 
Upvote 0
The examples in post #1 are not the same as the examples in post #4.

So your data always starts with 3 letters and an underscore?

SAM_
BOB_
etc_
 
Upvote 0
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
The examples in post #1 are not the same as the examples in post #4.

So your data always starts with 3 letters and an underscore?

SAM_
BOB_
etc_
Sorry, i thought the start don't matter because what I want to remove only what's at the end.

the list has different starts and length, could be anything. but all at the end of the text, either doesn't have the "_number" or ends with "_number" and the number could be either one digit or 2 digits.

what i want, regardless of what the list start with or the length, i just want to remove anything ends with underscore and any number after it.

so, at the end all will be without underscore number (_number) in the end.




hope i clarified it well :) and sorry again.
 
Last edited:
Upvote 0
How about
Excel Formula:
=IF(LEN(TEXTAFTER(A2,"_",-1,,1))<4,TEXTBEFORE(A2,"_",-1),A2)
 
Upvote 1
Solution
Glad we could help & thanks for the feedback.
 
Upvote 1

Forum statistics

Threads
1,215,334
Messages
6,124,325
Members
449,154
Latest member
pollardxlsm

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