Solved

VBA, Remove last portion of a string that matches the format "*nn/12"

Posted on 2011-09-06
8
204 Views
Last Modified: 2012-05-12
Here is the String:

Example1: MyString = "VariableContent*nn/12" (nn must me any number)
Example2: Mystring = "Rapids 12 month Ratio: R*9/  12"
Example3: MyString = "Cats food yearly *8 *4 /12"
Example4: MyString = "*Yearly Ratio * 11/12    "


I just want to remove the last portion of it but only if it is of the format "*n/12" where n can be any number from 0. * is the plain asterix symbol "*"

Any Ideas?

Thanks in advance...
0
Comment
Question by:New_Alex
8 Comments
 
LVL 29

Expert Comment

by:gowflow
ID: 36487989
You want to remove everything after the * ?
=MID(A1,1,FIND("*",A1,1)-1) if all your examples in Col A
if it is not that pls clarify by what you expect the result to be
gowflow
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 200 total points
ID: 36488331
Here is regex code which should accommodate:

Dim reg As New RegExp

With reg
    .Pattern = "\*\s*\d+/\s*\d+\s*$"
    sourceData = .Replace(sourceData, "")
End With

Open in new window

0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36488372
I was just about to post the regexp solution as well, but kaufmed has got there first - don't forget that to make it work you'll need to add a reference to Microsoft VBScript Regular Expressions 5.5
0
 
LVL 10

Accepted Solution

by:
plummet earned 100 total points
ID: 36488496
How about:

function stripEndStuff(sInput as string) as string

    if right$(sInput,3)="/12" and len(sInput)>3then 
        stripEndStuff=left$(sInput,instr(sInput,"*")-1)
    else
        stripEndStuff=sInput
    end if

end function

Open in new window


This will return the string without the * nn/12 if the string ends with /12.

Hope it helps!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

Assisted Solution

by:Bill Prew
Bill Prew earned 200 total points
ID: 36490055
Since you posted this in a regex zone, here an example of a small function that would return the matching part you want using RegEx. Let me know what questions you have...

Function DoRegEx(s As String)
    Dim r As Object
    Dim c As Object
    
    Set r = CreateObject("vbscript.regexp")
    With r
        .MultiLine = False
        .Global = True
        .IgnoreCase = True
        .Pattern = "\*\s*(\d+/\d+$)"
    End With
    Set c = r.Execute(s)
    If c.Count = 0 Then
        DoRegEx = ""
    Else
        DoRegEx = c(0).SubMatches(0)
    End If
End Function

Open in new window

~bp
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 36490063
Whoops, gotta refrsh more often...

~bp
0
 
LVL 1

Author Closing Comment

by:New_Alex
ID: 36508585
I adopted plummet solution even though it had many flows,  just to avoid RegEx.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 36509874
So I guess you ended up with something like this?

Function Extract(s)
  i = InStrRev(s, "*")
  If i > 0 Then Extract = Trim(Mid(s, i + 1))
End Function

Open in new window

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

867 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

15 Experts available now in Live!

Get 1:1 Help Now