Consulting

Results 1 to 5 of 5

Thread: Removing parts before each item

  1. #1

    Removing parts before each item

    Suppose I have a text string like these:

    Scenario 1:
    5-15 parts of bean pulp, 50.5 parts of rapeseed meal, 20-100 parts of peanut, 30 parts of corn

    Scenario 2:
    5-15 pts. wt. bean pulp, 50.5 pts. wt. rapeseed meal, 20-100 pts. wt. peanut, 30 pts. wt. corn

    Scenario 3:
    5-15 weight% bean pulp, 50.5 weight% rapeseed meal, 20-100 weight% peanut, 30 weight% corn

    How to I remove the parts (and their values) and leave the string with only the items, like this one?

    Desired output:
    bean pulp, rapeseed meal, peanut, corn
    Last edited by swaggerbox; 02-10-2020 at 05:05 AM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    probably not 100% bullet proof


    Option Explicit
    
    
    Sub drv()
        Dim s1 As String, s2 As String, s3 As String
    
    
        s1 = "5-15 parts of bean pulp, 50.5 parts of rapeseed meal, 20-100 parts of peanut, 30 parts of corn"
        s2 = "5-15 pts. wt. bean pulp, 50.5 pts. wt. rapeseed meal, 20-100 pts. wt. peanut, 30 pts. wt. corn"
        s3 = "5-15 weight% bean pulp, 50.5 weight% rapeseed meal, 20-100 weight% peanut, 30 weight% corn"
        
        'bean pulp, rapeseed meal, peanut, corn
        MsgBox JustItems(s1)
        MsgBox JustItems(s2)
        MsgBox JustItems(s3)
        
    End Sub
    
    
    
    
    Function JustItems(s As String) As String
        Dim s1 As String
        Dim v As Variant
        Dim i As Long
        
        v = Split(s, " ")
    
    
        For i = LBound(v) To UBound(v)
            If v(i) Like "*[0-9]*" Then
                v(i) = vbNullString
            ElseIf LCase(v(i)) = "of" Then
                v(i) = vbNullString
            ElseIf LCase(v(i)) = "of" Then
                v(i) = vbNullString
            ElseIf InStr(v(i), "part") Then
                v(i) = vbNullString
            ElseIf InStr(v(i), "pt") Then
                v(i) = vbNullString
            ElseIf InStr(v(i), "weight") Then
                v(i) = vbNullString
            ElseIf InStr(v(i), "wt") Then
                v(i) = vbNullString
            End If
        Next i
        
        s1 = Join(v, " ")
        
        JustItems = Replace(s1, "  ", " ")
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    wow, just amazing Paul. Thank you

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Sub snb()
      s1 = "5-15 parts of bean pulp, 50.5 parts of rapeseed meal, 20-100 parts of peanut, 30 parts of corn"
      s2 = "5-15 pts. wt. bean pulp, 50.5 pts. wt. rapeseed metal, 20-100 pts. wt. peanut, 30 pts. wt. corn"
      s3 = "5-15 weight% bean pulp, 50.5 weight% rapeseed meal, 20-100 weight% peanut, 30 weight% coorn"
        
      sn = Split("pulp_rapeseed meal_peanut_corn", "_")
    
      For j = 1 To 3
        s4 = Choose(j, s1, s2, s3)
        MsgBox Join(Filter(Array(IIf(InStr(s4, sn(0)), sn(0), "~"), IIf(InStr(s4, sn(1)), sn(1), "~"), IIf(InStr(s4, sn(2)), sn(2), ""), IIf(InStr(s4, sn(3)), sn(3), "~")), "~", 0), "_")
      Next
    End Sub

  5. #5
    An alternative path, thanks snb.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •