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
Solved

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

Posted on 2011-09-06
8
209 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
LVL 53

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 53

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 53

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

856 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