Solved

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

Posted on 2011-09-06
8
211 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 30

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 55

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 55

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 55

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

691 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