Solved

How to select text in Excel 2010 like MID function but from end of text rather than beginning?

Posted on 2016-09-25
3
32 Views
Last Modified: 2016-09-25
Instead of MID which starts at the beginnng of a line of text, the formula or user-defined function that would start at the end of the text and search toward the beginning.  I need to select from the end of the long text up to dash for a result of: View > Windows > Arrange All

Arrange All - Tile all open program windows side-by-side on the screen. - View > Window > Arrange All
0
Comment
Question by:Alex972
  • 2
3 Comments
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
ID: 41815060
Worksheet formula to get text from last hyphen in A1 through end of text in cell (up to 99 characters):
=MID(A1,FIND("|",SUBSTITUTE(A1,"-","|",LEN(A1)-LEN(SUBSTITUTE(A1,"-",""))))+1,99)

The formula works by counting the number of hyphens as original length less length with hyphens deleted by SUBSTITUTE. It then uses another SUBSTITUTE function to replace the last hyphen with a pipe | character. It then uses the FIND function to locate the position of the pipe character. Using that location, the MID function then returns the desired text.

I made no attempt to remove the space following the hyphen, though I could have done so by adding a TRIM function to the result.
0
 

Author Closing Comment

by:Alex972
ID: 41815069
Great, thanks
0
 
LVL 80

Expert Comment

by:byundt
ID: 41815074
If you prefer a VBA user-defined function, consider the following code. Install it in a regular module sheet (just like a macro), then use a worksheet formula like:
=LastDashToEnd(A1)

As written, the user-defined function removes leading and trailing spaces with the VBA Trim function. The VBA Trim function does not remove space characters that are repeated in the interior of the returned string.

In case you might want to repurpose the user-defined function for a character (or string) other than a hyphen, I included an optional parameter in the function where you can specify such.
Function LastDashToEnd(SomeText As String, Optional FindText As String = "-") As String
'Returns text from SomeText starting after the last instance of FindText. Returned text continues to end of SomeText.
'Use with worksheet formula like: _
    =LastDashToEnd(A1)
'If you want to specify a different character (or string) to find in SomeText, use the FindText parameter like this: _
    =LastDashToEnd(SomeText, "#")         returns text starting with last # in SomeText
Dim n As Long
n = InStrRev(FindText & SomeText, FindText)
LastDashToEnd = Trim(Mid(FindText & SomeText, n + Len(FindText)))
End Function

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now