+ Reply to Thread
Results 1 to 13 of 13

How to Trim a string

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    How to Trim a string

    Hi Everyone,

    I need a trim function that will do the following thing.

    For example consider i'm assigning this line "Value - $10 for pickup" to a variable. I need to get that $10 alone from that line and assign to another variable. this line varies from time to time. sometimes it will be like "After Pickup $5,000 for drop to home". So what I need is i need to extract the amount alone that is $10, $5,000 from any line and place it in a variable. How to handle this. Any Idea. Thanks in advance.

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: How to Trim a string

    for example
    Sub ert()
    Dim s As String, txt As String
    s = ActiveCell.Value
    With CreateObject("VBScript.RegExp")
        .Pattern = "\$[0-9]+,?[0-9]+" ' .Pattern = "\$\d+,?\d+"
        If .Test(s) Then
            txt = .Execute(s)(0)
        End If
        MsgBox txt
    End With
    End Sub
    Last edited by nilem; 01-16-2013 at 11:01 AM. Reason: add comment

  3. #3
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to Trim a string

    Quote Originally Posted by nilem View Post
    for example
    Sub ert()
    Dim s As String, txt As String
    s = ActiveCell.Value
    With CreateObject("VBScript.RegExp")
        .Pattern = "\$[0-9]+,?[0-9]+" ' .Pattern = "\$\d+,?\d+"
        If .Test(s) Then
            txt = .Execute(s)(0)
        End If
        MsgBox txt
    End With
    End Sub
    Thank You for the reply. I posted my concern in the previous post.Please let me know if you need any additional inputs

  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: How to Trim a string

    If your Text is in A1 cell use this formula

    =MID(A1,FIND("$",A1),FIND(" ",A1,FIND("$",A1))-FIND("$",A1))

  5. #5
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: How to Trim a string

    If your Text is in A1 cell use this formula

    =MID(A1,FIND("$",A1),FIND(" ",A1,FIND("$",A1))-FIND("$",A1))

  6. #6
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to Trim a string

    Quote Originally Posted by kvsrinivasamurthy View Post
    If your Text is in A1 cell use this formula

    =MID(A1,FIND("$",A1),FIND(" ",A1,FIND("$",A1))-FIND("$",A1))
    Hi. Thank you for the reply. I would like to let you know that i'm not taking the text from a cell in excel sheet. I'm taking the text from a word document and assigning it to a variable and then passing that variable to an excel cell. so inbetween i want a trim function to happen. I'm attaching the code for your reference. This should throw some light.

    Sub FindAndCopyNext()
    
        Dim TextToFind As String, TheContent As String, Trm As String, wdApp As Object
        Dim rng As Word.range
        Dim objWord As Object
        Dim oExcel As Object
        Dim oSheet As Object
        Dim oBook As Object
        Dim sExcelPath As String
        Dim range As Object
    
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
        objWord.Documents.Open "C:\Documents and Settings\Prasad\Desktop\ABC\Sample.doc"
    
        TextToFind = "Lowest-Cost Option" 'the text you're looking for to
                              ' locate the other content
                              
                              
    
        Set rng = ActiveDocument.Content
        rng.Find.Execute FindText:=TextToFind, Forward:=True
    
        If rng.Find.Found Then
            If rng.Information(wdWithInTable) Then
              TheContent = rng.Cells(1).Next.range.Text      'move right on row
              'TheContent = rng.Cells(1).Previous.Range.Text 'move left on row
              MsgBox "Found content '" & TheContent & "'"
            End If
        Else
            MsgBox "Text '" & TextToFind & "' was not found!"
        End If
    
    
    Set oSheet = ActiveWorkbook.Worksheets(2)
    Set range = oSheet.UsedRange
    
    
    'Need to trim 'TheContent' here before passing to excel cell. Lets say 'TheContent' has a value "After Pickup $5,000 for drop to home". I need to pass only $5,000
    
    range.Cells(2, 9) = TheContent
    
    End Sub

  7. #7
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: How to Trim a string

    In your code Change this line


    range.Cells(2, 9) = TheContent

    as

    range.Cells(2, 9) = =Mid(TheContent, InStr(1, TheContent, "$"), InStr(InStr(1, TheContent, "$"), TheContent, " ") - InStr(1, TheContent, "$"))

  8. #8
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to Trim a string

    Quote Originally Posted by kvsrinivasamurthy View Post
    In your code Change this line


    range.Cells(2, 9) = TheContent

    as

    range.Cells(2, 9) = =Mid(TheContent, InStr(1, TheContent, "$"), InStr(InStr(1, TheContent, "$"), TheContent, " ") - InStr(1, TheContent, "$"))
    That's awesome piece of code. works like charm.
    I have one more thing to say you.
    sometimes there will be % instead of $. For example I will have a line like "30% commission, after drop". So in this case what i'm supposed to do. I need to seperate that 30% alone from the whole sentence. Thanks in advance

  9. #9
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: How to Trim a string

    Will the figure like 30% appear always in the begining.

  10. #10
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to Trim a string

    Quote Originally Posted by kvsrinivasamurthy View Post
    Will the figure like 30% appear always in the begining.
    No. I just noted that in most of the cases it will be in the begining. But there are chances it might come in the middle as well.
    Last edited by sugaprasad; 01-17-2013 at 07:46 AM.

  11. #11
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: How to Trim a string

    After this line

    'Need to trim 'TheContent' here before passing to excel cell. Lets say 'TheContent' has a value "After Pickup $5,000 for drop to home". I need to pass only $5,000
    Pl add this code


    Dim T As Integer
    Dim M As Variant
    
    M = Split(TheContent, " ")
    
    For T = 0 To UBound(M)
    If InStr(1, M(T), "%") > 0 or InStr(1, M(T), "$") > 0 Then
    TheContent = M(T)
    Exit For
    End If
    Next T
    Before this line

    range.Cells(2, 9) = TheContent
    This will extract words containing % or $.
    Last edited by kvsrinivasamurthy; 01-18-2013 at 04:22 AM.

  12. #12
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to Trim a string

    Quote Originally Posted by kvsrinivasamurthy View Post
    After this line

    'Need to trim 'TheContent' here before passing to excel cell. Lets say 'TheContent' has a value "After Pickup $5,000 for drop to home". I need to pass only $5,000
    Pl add this code


    Dim T As Integer
    Dim M As Variant
    
    M = Split(TheContent, " ")
    
    For T = 0 To UBound(M)
    If InStr(1, M(T), "%") > 0 or InStr(1, M(T), "$") > 0 Then
    TheContent = M(T)
    Exit For
    End If
    Next T
    Before this line

    range.Cells(2, 9) = TheContent
    This will extract words containing % or $.
    Wow..that's simply awesome. Its working great. Thank you so much for your help. I'm going to mark this thread as solved.

  13. #13
    Forum Contributor
    Join Date
    11-29-2012
    Location
    Coimbatore, India
    MS-Off Ver
    Excel 2010
    Posts
    101

    Re: How to Trim a string

    Quote Originally Posted by kvsrinivasamurthy View Post
    After this line

    'Need to trim 'TheContent' here before passing to excel cell. Lets say 'TheContent' has a value "After Pickup $5,000 for drop to home". I need to pass only $5,000
    Pl add this code


    Dim T As Integer
    Dim M As Variant
    
    M = Split(TheContent, " ")
    
    For T = 0 To UBound(M)
    If InStr(1, M(T), "%") > 0 or InStr(1, M(T), "$") > 0 Then
    TheContent = M(T)
    Exit For
    End If
    Next T
    Before this line

    range.Cells(2, 9) = TheContent
    This will extract words containing % or $.
    This Code is working great. But i'm facing one problem here. Suppose if there is no space between $ value and the next word its not triming the $value alone.

    For example consider there is a line "After Pickup $5,000for drop to home"

    In the above sentence there is no space between $5000 and for. So $5000for is getting assigned to the TheContent variable instead of $5000.
    I would like to assign only the number value to TheContent variable. Can you please help me for one more time.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1