Solved

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

Posted on 2011-09-06
8
199 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
Comment Utility
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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 200 total points
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
Whoops, gotta refrsh more often...

~bp
0
 
LVL 1

Author Closing Comment

by:New_Alex
Comment Utility
I adopted plummet solution even though it had many flows,  just to avoid RegEx.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

763 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

12 Experts available now in Live!

Get 1:1 Help Now