+ Reply to Thread
Results 1 to 6 of 6

vba proc for a replace function using wildcard

  1. #1
    Forum Contributor
    Join Date
    08-21-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    255

    vba proc for a replace function using wildcard

    I require vba proc for a replace function to be able to specify a find string using a known prefix & a known suffix with a wildcard for the unknown portion (wildcard will always be between the prefix and suffix).

    And replace all instances of find criteria within text and replace with replace text (in this example replace text = nothing)
    The find string may exist 1, many or no time within the text string.

    Ideally I would like the macro to accommodate eith * or ? wildcards.

    Text = “abc 1234 def xyz abc 1234 def”
    Replace = “”


    Eg 1
    Find = “bc*de”
    Result = “abcdef xyz abcdef”

    Eg 2
    Find = “bc 1*4 de”
    Result = “abc 14 def xyz abc 14 def”

    Eg 3
    Find = “bc 1?34 de”
    Result = “abc 134 def xyz abc 134 def”

    Eg 4
    Find = “bc 1??? de”
    Result = “abc 1 def xyz abc 1 def”

    Eg 5
    Find = “bc ?? de”
    Result = “abc 1234 def xyz abc 1234 def” (match not found)

    Any help would be appreciated
    Last edited by shg; 04-22-2010 at 12:29 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: vba proc for a replace function using wildcard

    Hello wotadude,

    Your best option is to use Regular Expressions. If you can post a sample of the data and the types of matches and replacements you want, I can help you with the coding.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    08-21-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    255

    Re: vba proc for a replace function using wildcard

    please refer to the files attatched.
    text file containing text for find and replace.
    and workbook with find string/pattern and replace with data.
    Some replacements will be blank (replace with nothing).
    There may be any number of matches (or zero)
    The workbook contains the code to read the text file, find and replace in the conventional manner for replacements where the find does not contain wildcards.
    So i need some code to process finds where they contain a wildcard.
    The wildcard pattern will be numerous, but the wildcard/s will always be within the find string.
    Care will be taken when establishing the actual wildcard find strings so the are not compounding a replacement to be replaced by another find and replace iteration if that makes sense.

    Your assistance with a regular expressions approach to deal with the wildcard finds would be much appreciated.
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: vba proc for a replace function using wildcard

    Hello wotadude,

    Thanks for the files. After looking at the your post and the workbook, I am going to use the matching method in your post because it is consistent with the wildcard meanings. This is an interesting challenge.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: vba proc for a replace function using wildcard

    Hello wotadude,

    Thanks again for the bump. I need you to clarify how to interpret the wild card characters. This post and the workbook use the characters differently. The standard meaning of "?" is to match any single character and the asterisk "*" is zero or more characters. The number or pound sign "#" is used to match a single digit.

  6. #6
    Forum Contributor
    Join Date
    08-21-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    255

    Re: vba proc for a replace function using wildcard

    I expect to only use * or ? or combination of.
    Also assume wildcard/s to be in one continuous position within the find string (123?*abc or 123?*abc not 12?3*abc or ?*123abc).
    I hope that clarifies you point.
    Thanks

+ 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