Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

How to adapt this custom Excel 2010 function to use for other character than just dash?

Posted on 2016-10-28
Medium Priority
52 Views
I had this question after viewing Would like this function to be made to work like a MID function, but in reverse..

Function GetDash(r As Range) As String
Dim intPos As Integer
intPos = InStrRev(r, "-")
GetDash = Mid(r, intPos - 1)
End Function
0
Question by:Alex Campbell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2
• 2
• +1

LVL 43

Expert Comment

ID: 41864184
Function GetDash(r As Range, s as string) As String
Dim intPos As Integer
intPos = InStrRev(r, s)
GetDash = Mid(r, intPos - 1)
End Function

and call it like

=getdash(a1,"@")
0

LVL 49

Expert Comment

ID: 41864185
``````Function GetDash(r As Range) As String
Dim intPos As Integer
intPos = InStrRev(r, "-")
GetDash = Mid(r, intPos - 1)
End Function
``````
Change the "-" in line 3 to something else or
``````Function GetDash(r As Range) As String
Dim intPos As Integer
Dim strChar As String

strChar = Inputbox("Enter desired character")
intPos = InStrRev(r, strChar)
GetDash = Mid(r, intPos - 1)
End Function
``````
0

LVL 43

Expert Comment

ID: 41864207
You can improve this to

Function GetDash(r As Range, Optional s As String = "-") As String
Dim intPos As Integer
intPos = InStrRev(r, s)
GetDash = Mid(r, intPos - 1)
End Function

so dash (-) is the default value if the second argument is not given
1

LVL 49

Expert Comment

ID: 41864210
Nice idea Syed.
0

LVL 35

Expert Comment

ID: 41864231
Note: If the String variable s does not exist within String variable r &, hence, intPos will be zero (0), then the following statement will produce a run-time error:

GetDash = Mid(r, intPos - 1)
0

LVL 1

Author Comment

ID: 41864424
I added these different functions to a module where I have a working function.
I tried the dash and other values, but I only get #VALUE! or blank.
0

LVL 1

Accepted Solution

Alex Campbell earned 0 total points
ID: 41866107
I appreciate the solutions suggested, but I searched elsewhere and found this:

Function Reverse(str As String) As String
Reverse = StrReverse(Trim(str))
End Function

I like this because it can be used with MID, REPLACE and SUBSTITUTE.

A1 formula =reverse(MID(reverse(B1),1,FIND("-",reverse(B1))-2))
A1 results   =Formulas > Formula Auditing > Trace Precedents

B1 value     =Show arrows that indicate what cells affect the value of the currently selected cell. - Formulas > Formula Auditing > Trace Precedents
reverse-mid.jpg
0

LVL 1

Author Closing Comment

ID: 41873682
The functions that were submitted didn't work for me.  I searched other sites and found a workable solution.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month5 days, 19 hours left to enroll