VBA Return Last Text from a String

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Is it possible to get the variable last text in a string after an underscore. There will always be an underscore before the last word but the length of the text is variable and the text contains letters and numbers. I would like to offset the returned text i.e C2 contains the text and G2 is where I would like to return the last text of the string. Can someone help with this please
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Assuming the value is in cell A1 then you may try this below VBA code in immediate window:

Code:
split([A1],"_")(len([A1])-len(replace([A1],"_","")))

or

Code:
split([A1],"_")(ubound(split([A1],"_")))
 
Last edited:
Upvote 0
Or a formula:

=TRIM(RIGHT(SUBSTITUTE(C2,"_",REPT(" ",LEN(C2))),LEN(C2)))
 
Upvote 0
Assuming the value is in cell A1 then you may try this below VBA code in immediate window:

Code:
split([A1],"_")(len([A1])-len(replace([A1],"_","")))

or

Code:
split([A1],"_")(ubound(split([A1],"_")))

Or...

Mid([A1], InStrRev([A1], "_") + 1)
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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