+ Reply to Thread
Results 1 to 19 of 19

How to REGex with VBA words

  1. #1
    Registered User
    Join Date
    11-27-2012
    Location
    Teplice
    MS-Off Ver
    2010
    Posts
    71

    How to REGex with VBA words

    Hi,
    I would like to ask if and how i can work with VBA in Excel, when i want replace characters and words like cases bellow.
    I were used to use notepad++ for doing this operations, but now i would like to check if Excel will help me too with this cases and how.
    Please give me some tips how to do it, or if it is possible at all....
    Howerver i did one post here and got answer to add a new post. There were only answer about renaming words that i know forwardly.. But not if i want to replace all [abcdefgh][0123456][0123456]whatever else words with for example [abcdefgh][0123456]0 so as it is described bellow / it is better described.

    thank you so much :-)


    Example:

    In Column A row 1:xxxxx find all words starting

    A1253
    change to (or make the result in Column B)
    A1200

    in Notepad++ i were doing this with regular expression mode like this
    find what ([ABCDEFGHJKLMNOPQRSTUVWXYZ])([0123456789][0123456789])[0123456789][0123456789]
    replace with \1\200

    or
    A1253-7555 xxx
    change to (or make the result in Column B)
    A1200

    in Notepad++ i were doing this with regular expression mode like this
    find what ([ABCDEFGHJKLMNOPQRSTUVWXYZ])([0123456789][0123456789])[0123456789][0123456789](.*)
    replace with \1\200


    Or
    some text 1253-xxx
    change to (or make the result in Column B)
    some text 1200

    in Notepad++ i were doing this with regular expression mode like this
    find what (.*)([0123456789][0123456789])[0123456789][0123456789]-(.*)
    replace with \1\200

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

    Re: How to REGex with VBA words

    Few example with the result will explain better, preferably a workbook.

  3. #3
    Registered User
    Join Date
    11-27-2012
    Location
    Teplice
    MS-Off Ver
    2010
    Posts
    71

    Re: How to REGex with VBA words

    huh, Ok, I thought it is obvious.. i dont know how to add here some sheet, but here it is..

    (A cells) What I need to convert (B cells) Form of converted text (I expect to find all cells containing first letter "a" or "b" and continues with four numbers and replace them with same letter a or b and replace last 2 numbers with "00"
    a5000 a5000
    a5003 a5000
    a5124 a5100
    b2000 b2000
    b2050 b2000
    b2150 b2100
    ab2152 ab2152
    a215 a215

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

    Re: How to REGex with VBA words

    Just for studying purpose.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-27-2012
    Location
    Teplice
    MS-Off Ver
    2010
    Posts
    71

    Re: How to REGex with VBA words

    Nice
    This regular expressions i can do.. i have done some tests and there are some questions i would like you to help me with... pleaseeee

    1) The thing is, that if i put this code (i need to do many cycles in one sheet):
    Please Login or Register  to view this content.


    Then second cycle overwrites the first. I think that this is about function .replace but i dont know the functions of VBA so here i would like you to give me advice of how to prevent replacing cycles. By other words... how to save every cycle in B columns...

    2) The other thing is, that i did not found information of how is working replace with function ($1).
    What are all possibilities of how to use replace please?
    Last edited by Odeen; 11-30-2012 at 12:35 PM.

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

    Re: How to REGex with VBA words

    First of all, vba codes MUST be wrapped with the code tag, so you need to edit your last post.
    i.e
    [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code]

    1) The first pattern should pick up "text A1" where A would be any character from A to Z, and 1 would be any one digit number.

    The second pattern will pick up additional one digit number and add "00" at the end.

    2) Replace method of RegExp works mainly 2 ways.
    If you use it with $number, it will retrieve the part that is backward referenced by a set of brackets.
    So, pattern has one set of brackets, $1 refers to (text [A-Z]\d{1}). <--{1} no need just \d is fine.
    And when the string doesn't match to the pattern, Replace method will return whole string.

    And RegExp have Match object and SubMatches object.

    If you google with the key word like

    VBA RegExp Meta Character

    You will find many good site.
    Last edited by jindon; 11-30-2012 at 12:29 PM.

  7. #7
    Registered User
    Join Date
    11-27-2012
    Location
    Teplice
    MS-Off Ver
    2010
    Posts
    71

    Re: How to REGex with VBA words

    Code is in...

    i understand of how to search the regex... of course i need to teach myself a lot...

    but in VBA is the thing, that i really cannot do these two procedures, to paste the renamed B cells. One procedure overwrites the other. How to do it please?

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

    Re: How to REGex with VBA words

    If you don't overwrite, just change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    Is this what you want to do?

  9. #9
    Registered User
    Join Date
    11-27-2012
    Location
    Teplice
    MS-Off Ver
    2010
    Posts
    71

    Re: How to REGex with VBA words

    yes, but i want all in A(,2)

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

    Re: How to REGex with VBA words

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    11-27-2012
    Location
    Teplice
    MS-Off Ver
    2010
    Posts
    71

    Re: How to REGex with VBA words

    Please Login or Register  to view this content.
    so this is not functional.. according to your code i dont see that there is setted another .pattern
    I need all possible patterns to replace in different .replace values - all in A(,2)

    If i do what i sent here, the second cycle overwrites the first. And still your last code i possible dont understand or you did not understand me well..

    Please try to make me suggestion. I will very apreciate it.

    Thank you so much.

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

    Re: How to REGex with VBA words

    What do you really want to do?

    Isn't the last code I posted working?

    Show me the raw data and the result that you want, otherwise, just wasting time for both...

  13. #13
    Registered User
    Join Date
    11-27-2012
    Location
    Teplice
    MS-Off Ver
    2010
    Posts
    71

    Re: How to REGex with VBA words

    So for example this

    Text 9200G Text 9200
    Text A2500H Text A2500
    Text A2G Text A2
    Text A2H Text A2
    Text A2K Text A2
    Text A2L Text A2
    Text A2S Text A2
    Text A2T Text A2
    Text A3000 Text A3000
    Text A3500E Text A3000
    Text A3500L Text A3000
    Text A3A Text A3
    Text A3AC Text A3
    Text A3E Text A3
    Text A3E Text A3
    Text A3H Text A3
    Text A3HF Text A3
    Text A3VP Text A3
    Text A4 Text A4
    Text A4000 Text A4000
    Text A5EB Text A5
    Text A5EC Text A5
    Text A5EC Text A5
    Text A5L Text A5
    Text A6 Text A6
    Text A6000 Text A6000
    Text A6000J Text A6000
    Text A6000JA Text A6000
    Text A6000KM Text A6000
    Text A6000KT Text A6000
    Text A6000VA Text A6000
    Text A6000VM Text A6000
    Text A6727G Text A6200
    Text A6727G Text A6200
    Text A6AV Text A6
    Text A6B00NE Text A6
    Text A6B00U Text A6
    Text A6F Text A6
    Text A6G Text A6
    Text A6GA Text A6
    Text A6J Text A6
    Text A6JC Text A6

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

    Re: How to REGex with VBA words

    Need logically understandable explanation why the following should return

    Text A3500E Text A3000
    Text A3500L Text A3000

    and

    Text A6727G Text A6200
    Text A6727G Text A6200

    whereas

    Text A2500H Text A2500

  15. #15
    Registered User
    Join Date
    11-27-2012
    Location
    Teplice
    MS-Off Ver
    2010
    Posts
    71

    Re: How to REGex with VBA words

    oh, sorry, that was mistake...

    Text A3500E to Text A3500
    Text A3500L to Text A3500

    and

    Text A6727G to Text A6700
    Text A6727G tp Text A6700

    whereas (this is right)

    Text A2500H Text A2500


    In other words, the regex script is working right, it is replacing as i want. But it is working right one or other.. but not one and other... try to put it to excel and you will see, that the values will be replaced right in first cycle for replacing all Text A6xxx to Text A6 but in second cycle this text will return to its previous state Text A6xxx.
    Last edited by Odeen; 12-01-2012 at 10:24 AM.

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

    Re: How to REGex with VBA words

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    11-27-2012
    Location
    Teplice
    MS-Off Ver
    2010
    Posts
    71

    Re: How to REGex with VBA words

    Oh this works great.

    Maybe i wanted to find solution of how to give here much more Else... it is doing by ElseIf, or? I tryed now doing some combinations, but none works.

    I will need to do here much more RegEx replacings... please one last figure out by proffesional. Please make me example how to add some more RegEx replaces in one macro.

    So maybe one more with
    Please Login or Register  to view this content.
    I think we will be at finish of this task and i am at least now very thankfull to your help.

    Thanks and wishing great days.

  18. #18
    Registered User
    Join Date
    11-27-2012
    Location
    Teplice
    MS-Off Ver
    2010
    Posts
    71

    Re: How to REGex with VBA words

    Oh this works great.

    Maybe i wanted to find solution of how to give here much more Else... it is doing by ElseIf, or? I tryed now doing some combinations, but none works.

    I will need to do here much more RegEx replacings... please one last figure out by proffesional. Please make me example how to add some more RegEx replaces in one macro.

    So maybe one more with
    Please Login or Register  to view this content.
    I think we will be at finish of this task and i am at least now very thankfull to your help.

    Thanks and wishing great days.

  19. #19
    Registered User
    Join Date
    11-27-2012
    Location
    Teplice
    MS-Off Ver
    2010
    Posts
    71

    Re: How to REGex with VBA words

    Ok,
    I have found out the solution and giving here its content. Maybe it will be helpfull to someone other.

    I will process some regex tasks from column A to B

    Anyway, thanks to Jindon and his great care of this case.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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