Results 1 to 7 of 7

Thread: Select text between brackets

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    9

    Select text between brackets

    Hello, I have a long text with references inside brackets that I now want to convert into footnotes (in Word). To find the references easily, I have put a hidden @ immediately after the first bracket. I want to write a macro that finds the @, moves at right one character and from there searches for the ending bracket by highlighting the text between the brackets. Once this is made, it will be easy to program the rest. The part I don't know how to achieve is how to highlight the text between the brackets in order to cut it. Any ideas?
    Last edited by Huldrich; Oct 29th, 2024 at 11:52 AM.

  2. #2
    Member
    Join Date
    Mar 2006
    Posts
    46

    Re: Select text between brackets

    Use instr & mid.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    9

    Re: Select text between brackets

    Ok, thank you I will try this out.

  4. #4
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    395

    Re: Select text between brackets

    As an add-on

    Code:
        Dim example As String
        Dim extractedText As String
        
        example = "123456 sometext @[grab this] soke more text afterwards"
        
        ' first find the @ and take all the text to the right of it and the first bracket [
        ' + 2 is to not include the @[ in the extracted text value
        extractedText = Mid(example, InStr(example, "@") + 2)
        
        ' now just keep the text to the left of the next bracket
        extractedText = Left(extractedText, InStr(extractedText, "]") - 1)
    
        Sheet1.Cells(5, 1).Value = "The found text is: " & extractedText
    EDIT: Excel was used just to show the VBA to extract the text more easily display it
    Attached Images Attached Images  

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    9

    Re: Select text between brackets

    Many thanks jdelano.

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,782

    Re: Select text between brackets

    Or use Regex
    https://regex101.com/r/t8PgUR/1

    Teststring = "123456 sometext [@grab this] soke more text afterwards" (He mentioned the @ AFTER the first Bracket)

    Pattern = \[@(.*)\]
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    9

    Re: Select text between brackets

    I also thought I could use a regular expression. This will definitely work.

Posting Permissions

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



Click Here to Expand Forum to Full Width