Results 1 to 18 of 18

Thread: Find Replace - With wild cards

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Cool Find Replace - With wild cards

    Hi
    I have a small issue.
    I'd like to find occurances of square brackets [], or this brackets (), in a word document and check if number or alphabet is immediately preceeding or suffixing it without space and if so insert one space.

    Eg. my house[VBF]is in china
    This I want to change it to my house [VBF] is in china

    Eg. my house(VBF)is in china
    This I want to change it to my house (VBF) is in china

    yes I used the wild card option in more..
    I believe it goes something like this in Find section: [A-Za-z0-9]\(
    I get struck up in the Replace section:???????

    Thanks in advance...

    patience is not the ability to wait but the ability to keep a good attitude while waiting
    Last edited by VBFnewcomer; May 4th, 2011 at 11:51 PM. Reason: insufficient information

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Question Re: Find Replace - With wild cards

    several weeks...still no solution leave alone response
    unbelievable

    Also I discovered its something to do with Regular expressions...
    Last edited by VBFnewcomer; May 30th, 2011 at 01:46 AM.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find Replace - With wild cards

    i remember trying to do this some time ago, but i think my internet crashed or some such

    i believe you would have to use a VBA procedure to do this, only way i figured was to use replace to put space next to each bracket (before or after), then replace double spaces with single space, either in all instance, or when next to each bracket
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Exclamation Re: Find Replace - With wild cards

    use replace to put space next to each bracket (before or after),
    That exactly is the issue...
    check if number or alphabet is immediately preceeding or suffixing it without space and if so insert one space.
    how to add the space without deleting the existing character (num/alphabet) ???

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Find Replace - With wild cards

    Code:
    To use less brain power, an easy way is to replace 
    "[" with " [" 
    "]" with "] "
    "(" with " ("
    ")" with ") "
    and then at the end
    "  " with " "
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Find Replace - With wild cards

    anhn that was good response....but...
    check if number or alphabet is immediately preceeding or suffixing it is without space and if so insert one space

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Find Replace - With wild cards

    Try it, it will work: Not just number or alphabet but if not-a-space.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Find Replace - With wild cards

    I'll try..sure...also let me know how to add space before numbers...

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find Replace - With wild cards

    if you really need to only add the space where character is number or letter (so not punctuation or other brackets, or like!@#$$&#37;^&* etc), the you would need to use VBA to find each bracket, check the character before (or after), then add space as appropriate, then find next bracket and repeat process, you would need to use instr to do this, otherwise loop through every character, to determine if it is a bracket

    you can not do what you originally specified using words find /replace method
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Find Replace - With wild cards

    The current issue is slightly away from the main topic...but related...so please bear with me...
    only add the space where character is number
    Yes, but only if it already has no space.
    so not punctuation
    They are taken care of..
    Take a look at this...
    the age of the building is200years.
    so here we need to search out the whole number sequence in this eg. its 200 and add space before & after.
    Other combinations would be
    the age of the building is 200years.
    Here we add space only after 200
    the age of the building is200 years.
    Here we add space only before 200

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find Replace - With wild cards

    The current issue is slightly away from the main topic...but related..
    you can test this, it should do for both
    vb Code:
    1. Dim char As Range, chars As String, alpha As String, notnums as string
    2.  
    3. alpha = "ABCDEFGHIJKLNOPQRSTUVWXYZ0123456789"
    4. notnums "ABCDEFGHIJKLNOPQRSTUVWXYZ"
    5.  
    6. For Each char In ThisDocument.Content.Characters
    7.     Select Case char
    8.         Case "(", "["
    9.             If InStr(1, alpha, ThisDocument.Characters(char.Start), vbTextCompare) > 0 Then char.InsertBefore " "
    10.         Case ")", "]"
    11.             If InStr(1, alpha, ThisDocument.Characters(char.End + 1), vbTextCompare) > 0 Then char.InsertAfter " "
    12.         Case 0 To 9
    13.             If InStr(1, notnums, ThisDocument.Characters(char.Start), vbTextCompare) > 0 Then char.InsertBefore " "
    14.             If InStr(1, notnums, ThisDocument.Characters(char.End + 1), vbTextCompare) > 0 Then char.InsertAfter " "
    15.  
    16.     End Select
    17. Next
    minimal testing done, as checks every character in the document, it may be too slow, but it does test for all brackets and numbers, you can add any other required characters to strings for them to be included for spaces to be inserted
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Find Replace - With wild cards

    I added your code in a sub just for the 0-9 case (at present) and ran it..nothing happened. Then I put the break point at "Case 0-9" and found that the loop does not enter the line next to it....
    Code:
    Dim char As Range, chars As String, alpha As String, notNums As String
    
        alpha = "ABCDEFGHIJKLNOPQRSTUVWXYZ0123456789"
        notNums = "ABCDEFGHIJKLNOPQRSTUVWXYZ"
        
        For Each char In ThisDocument.Content.Characters
            Select Case char
                Case 0 To 9
                    If InStr(1, notNums, ThisDocument.Characters(char.Start), vbTextCompare > 0) Then char.InsertBefore " "
                    If InStr(1, notNums, ThisDocument.Characters(char.End + 1), vbTextCompare > 0) Then char.InsertAfter " "
            End Select
        Next
    BTW
    Code:
    notNums = "ABCDEFGHIJKLNOPQRSTUVWXYZ"
    did not have "=" so I added it...

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find Replace - With wild cards

    did not have "=" so I added it...
    typo
    Then I put the break point at "Case 0-9" and found that the loop does not enter the line next to it....
    works for me
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  14. #14
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Find Replace - With wild cards

    My logic in post#5 is:

    1. Always add a space before "[" and "(" regardless they are preceeded by a space or not.
    2. Always add a space after "]" and ")" regardless they are followed by a space or not.
    3. In both cases, if a space (before or after) already existed, it will become double spaces; so just replace all double spaces with single space.

    Easy!
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find Replace - With wild cards

    My logic in post#5 is:
    basically what i put in post #3, which did not completely fill the original request and did not cover the additional requirement in posts #8 /10
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Exclamation Re: Find Replace - With wild cards

    Dear Anhn & west,
    I did understand what you were saying and already implemented it in the same order as suggested by you....Thanks for your expert guidance.

    Ive included a sample file for your quick reference.

    In each word block (I believe its called simply word in Office VBA) one must check for series of alpahbets or numbers. If there exist no space after the end of series then we must add a space.
    eg. This document has200pages.
    Here has200pages is a block. First we identify the start series, in the present case 'has'
    this is followed by numbers ie. '200',
    there is no intervening space so we add one space
    this is followed by numbers ie. 'pages',
    here again there is no intervening space so we add one space

    Imp: Existing data must not be deleted
    Attached Files Attached Files

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Find Replace - With wild cards

    guys any more inputs.........

  18. #18
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find Replace - With wild cards

    as my testing for the code in post #11 appeared to work correctly i do not understand why it did not work for you
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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