?
Solved

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

Posted on 2011-09-06
8
Medium Priority
?
213 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
[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
  • Learn & ask questions
8 Comments
 
LVL 31

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 800 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Accepted Solution

by:
plummet earned 400 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
 
LVL 56

Assisted Solution

by:Bill Prew
Bill Prew earned 800 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 56

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 56

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

752 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