+ Reply to Thread
Results 1 to 5 of 5

Macro Help for Substrings

  1. #1

    Macro Help for Substrings

    All,

    I am looking to rid myself of extra information in a column, and doing
    so in a Macro

    Here is what I have in column A

    SELECT Column1, Column2 From Mydb.Myspace.My table Where Column1 =
    'Hello"

    I want to frind the FROM word (There is only one in the cell) and
    delete all to the Left including the from

    Then Find the WHERE (If it exsists) and delete all to the Right
    including the WHERE

    And I would end up with column A

    Mydb.Myspace.My table

    I have several examples of removing single char or commas from the
    column, but not entire words.

    Any ideas?

    Doug


  2. #2
    Ardus Petus
    Guest

    Re: Macro Help for Substrings

    Assuming your text is in A4, enter the following formula in B4:
    =TRIM(MID(A4,SEARCH("from",A4)+5,LEN(A4)-SEARCH("where",A4)))

    HTH
    --
    AP

    <[email protected]> a écrit dans le message de news:
    [email protected]...
    > All,
    >
    > I am looking to rid myself of extra information in a column, and doing
    > so in a Macro
    >
    > Here is what I have in column A
    >
    > SELECT Column1, Column2 From Mydb.Myspace.My table Where Column1 =
    > 'Hello"
    >
    > I want to frind the FROM word (There is only one in the cell) and
    > delete all to the Left including the from
    >
    > Then Find the WHERE (If it exsists) and delete all to the Right
    > including the WHERE
    >
    > And I would end up with column A
    >
    > Mydb.Myspace.My table
    >
    > I have several examples of removing single char or commas from the
    > column, but not entire words.
    >
    > Any ideas?
    >
    > Doug
    >




  3. #3
    Tom Ogilvy
    Guest

    RE: Macro Help for Substrings

    Here is a demo from the immediate window showing how to do it.

    You only need the final command:

    s = "SELECT Column1, Column2 From Mydb.Myspace.Mytable Where Column1 =
    'Hello'"
    ? s
    SELECT Column1, Column2 From Mydb.Myspace.Mytable Where Column1 = 'Hello'
    ? left(s,Instr(1,s,"Where",vbTextCompare)-1)
    SELECT Column1, Column2 From Mydb.Myspace.Mytable
    ?
    mid(s,instr(1,s,"From",vbTextCompare)+4,Instr(1,s,"Where",vbTextCompare)-(instr(1,s,"From",vbTextCompare)+4))
    Mydb.Myspace.Mytable

    --
    Regards,
    Tom Ogilvy


    "[email protected]" wrote:

    > All,
    >
    > I am looking to rid myself of extra information in a column, and doing
    > so in a Macro
    >
    > Here is what I have in column A
    >
    > SELECT Column1, Column2 From Mydb.Myspace.My table Where Column1 =
    > 'Hello"
    >
    > I want to frind the FROM word (There is only one in the cell) and
    > delete all to the Left including the from
    >
    > Then Find the WHERE (If it exsists) and delete all to the Right
    > including the WHERE
    >
    > And I would end up with column A
    >
    > Mydb.Myspace.My table
    >
    > I have several examples of removing single char or commas from the
    > column, but not entire words.
    >
    > Any ideas?
    >
    > Doug
    >
    >


  4. #4

    Re: Macro Help for Substrings



    Sub ExtactText()
    Dim nStart As Integer, nEnd As Integer
    Dim txtString As String, txtSearch As String

    'Next two lines create a test sample
    cells(1, 1) = "delete all of this FROM column a WHERE delete all of
    this."
    txtString = ActiveCell

    txtSearch = "FROM"
    nStart = InStr(1, txtString, txtSearch) + Len(txtSearch) + 1

    txtSearch = "FROM"
    nEnd = InStr(1, txtString, txtSearch)

    txtString = Mid(txtString, nStart, Len(txtString) - nStart - nEnd)

    ''Next line pastes results of test sample
    ActiveCell.Offset(0, 1) = txtString
    End Sub


  5. #5

    Re: Macro Help for Substrings

    Sorry, Sent the wrong one.

    Sub ExtactText()
    Dim nStart As Integer, nEnd As Integer
    Dim txtString As String

    txtString = ActiveCell

    nStart = InStr(1, txtString, "FROM") + 5
    nEnd = InStr(1, txtString, "WHERE")
    txtString = Mid(txtString, nStart, nEnd - nStart - 1)

    ActiveCell.Offset(0, 1) = txtString
    End Sub


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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