+ Reply to Thread
Results 1 to 14 of 14

Routine to remove brackets around words, leaving the format intact. Doesn't always work.

Hybrid View

  1. #1
    Registered User
    Join Date
    12-07-2012
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    81

    Routine to remove brackets around words, leaving the format intact. Doesn't always work.

    For JINDON

    Dear Jindon, you and others were kind enough to supply a routine for me that would take words in brackets in a sentence, which were coloured differently from the rest of a sentence, remove the brackets, and leave the colouring of the words intact.

    Original post:

    http://www.excelforum.com/excel-prog...63#post3146163


    I've included the original test sheet. It has 2 routines; ColourBrackets, which colours red any words in brackets, and RmBrackets, which is your routine, and which removes the brackets.

    However, it doesn't always work.

    If you take the test sentences I've supplied, and run ColourBrackets, you'll see that it works correctly, colouring all the bracketed words.

    But if you then run your routine, it removes the brackets, but colours ALL words in red in the case of one sentence, but works fine with the other!

    Lots of things work fine, but sometimes it does this. It is consistent. Run either sentence as often as you like and the result will be the same.

    There is obviously something tripping it up, but I'm blowed if I can see what it could be.

    Help with this would be very much appreciated.

    Regards.
    Attached Files Attached Files
    Last edited by staggers47; 04-29-2013 at 06:36 AM.

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    I removed the brackets from [Police] ran it, and was successful. This means that, at least initially, the culprit is the fact that the first character of the entire string is a bracket.

    Trap that scenario and I think you'll be fixed up.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    it willwork if you add this line of code before the last FOR-NEXT of the macro.
    It actually put all characters in color BLACK then the last part of the macro will color in RED the specific words found.
    r.Font.ColorIndex = 0
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

  4. #4
    Registered User
    Join Date
    12-07-2012
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    81

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    Thank you both. Yes, that worked p24.

    Still curious to know why it worked most of the time, but not always. I thought Alvero was onto something there, but I'm sure there have been other times it hasn't worked when the first word wasn't bracketed.

    Anyhow, it seems to be okay now, fingers crossed!

    Thanks again to you both.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    It suppose to color the words only if the original word in brackets are colored....

    Change to this.

    It will strip the words in brackets and color them in red.
    Sub RmBrackets()
        Dim r As Range, x(), i As Long
        With CreateObject("VBScript.RegExp")
            .Global = True
            For Each r In Range("L22")
                If r.Address = r.MergeArea.Cells(1).Address Then
                    .Pattern = "(\[)([^\[\]]+)(\])"
                    If .test(r.Value) Then
                        ReDim x(1 To .Execute(r.Value).Count, 1 To 3)
                        For i = 0 To .Execute(r.Value).Count - 1
                            x(i + 1, 1) = .Execute(r.Value)(i).firstindex + 1 - i * 2
                            x(i + 1, 2) = .Execute(r.Value)(i).Length - 2
                            x(i + 1, 3) = r.Characters(.Execute(r.Value)(i) _
                                .firstindex + 2, 1).Font.Color
                        Next
                        r.Value = .Replace(r.Value, Chr(2) & "$2" & Chr(2))
                        .Pattern = Chr(2)
                        r.Value = .Replace(r.Value, "")
                        For i = 1 To UBound(x, 1)
                            r.Characters(x(i, 1), x(i, 2)).Font.Color = vbRed
                        Next
                    End If
                End If
            Next
        End With
    End Sub

  6. #6
    Registered User
    Join Date
    12-07-2012
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    81

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    OK, got it.

    Thanks very much for your time.

    Regards.

  7. #7
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    To jindon,
    your last macro colors the selected words in RED but the main issue is that, when a sentence starts with a bracket, the macro changes the whole sentence color in the color for the bracketed words from the first macro ColourBrackets(). If that color was red, your macro will give the whole sentence in RED. Try it.
    It seems that removing the first bracket, changes the color of the whole cell to RED.

    That is why I inserted a line of code to color the whole cell in black before your macro is recoloring the bracketed words in red. It's only a patch but it does the job.

    I don't know why but this is what is going on here. Can you explain it?
    Thanks

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    I don't understand what you are talking about and is this your thread?

  9. #9
    Registered User
    Join Date
    12-07-2012
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    81

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    No it's mine. And I do understand what he's talking about.

    To go back to the beginning:

    I had a routine that coloured anything between brackets (and the brackets as well) in red. The routine ColourBrackets does that fine.

    I then needed to remove the brackets, LEAVING THE RED WORDS RED. That is the routine called RmBrackets, for which you supplied the code.

    I have now discovered that there are times that it doesn't work, so I came back here and asked you, Jindon, if you could solve the problem.

    In the meantime, p24 came up with a patch, which I tried, and it worked fine. So far I haven't been able to trip it up.

    You then came along with an updated version of your original code. I used that, and even with the bad sentence I supplied in the test book, it didn't work for me, and it obviously didn't for p24, which is why he mentioned it.

    So we have your original code, which didn't work in all cases, and the updated version, which still doesn't.
    And in both cases, it does work (so far!) with p24's patch included.


    However, with the patch it does work, so I'm happy.

    Would still be curious to know why the bracket at the beginning trips it up.

    Regards to you both.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    Can you show me the example of what you are talking.

    I can not replicate the situation.

  11. #11
    Registered User
    Join Date
    12-07-2012
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    81

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    Ok, well there's obviously no point sending you the workbook because you already have it!

    So all I can do is supply a picture of what's happening, which I have done.

    The question now becomes, then, why does this happen to ap24 and me, but not to you?

    Mysterious!

    Regards.
    Attached Images Attached Images

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    As I said I have no idea about what you are saying.

    It is working to me.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    12-07-2012
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    81

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    NOW I UNDERSTAND.

    Now I have back your version of my test book I see that you are not doing the same thing I am!

    I am using two routines. You are using one routine - the one you wrote - and ignoring the previous one.

    The way I did it was:

    Routine 1.
    ColourBrackets. Take the words in brackets and colour them. (So we finish with a sentence where the words are two different colours.)
    Routine 2.
    RmBrackets. Strip the brackets, and leave the red words red.

    What you are doing:

    Routine 1. RmBrackets
    Take a sentence where all the words start same colour (ignoring Routine 1), and remove brackets, making any words between them red. So you are not leaving words red, you are making them red.

    That is why ap24's patch worked, it made the sentence the colour your routine needs, rather than two different colours.

    And here's the good bit: if I leave out the previous routine too, your routine works!

    SO you've saved me a routine, which will speed things up nicely, and also the mystery is solved.

    Thank you.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Routine to remove brackets around words, leaving the format intact. Doesn't always wor

    OK, you seems to stick with the other routine...

    Glad we finally come to the end.

+ 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