Search code examples
excelregexvbanotepad++

How to make a macro that replace string in Excel


Does anyone know how to make a macro in Excel that convert strings ? I want to convert all strings existing in one column respecting this transformation :

initial result of macro
first_name $firstName
last_name $lastName
email $email
email2 $email2

I would like to transform in Uppercase after the underscore if it's a caracter.

Thanks a lot !


Solution

  • A Notepad++ solution:

    • Ctrl+H
    • Find what: (?:^([^\W_]+)|\G(?!^))(?:_([^\W_]*))?
    • Replace with: (?1\$$1)\u$2
    • CHECK Wrap around
    • CHECK Regular expression
    • Replace all

    Explanation:

    (?:             # non capture group
        ^               # beginning of line
        (               # group 1
            [^\W_]+         # 1 or more non non-word character or underscore
        )               # end group 1
      |               # OR
        \G(?!^)         # restart from last match position, not at the beginning of lie
    )               # end grroup
    (?:             # non capture group
        _               # underscore
        (               # group 2
            [^\W_]*         # 0 or more non non-word character or underscore
        )               # end group 1
    )               # end group
    

    Replacement:

    (?1             # if group 1 exists
        \$$1            # add a dollar before the group 1
    )               # endif
    \u$1            # uppercase the first letter of group 2
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here