Home » excel » Map a replacement string in Excel

Map a replacement string in Excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have an excel sheet with 2 columns: absolute path and relative path.
Now say the data is in below format:

 absolute path           relative path
 -------------           -------------
 /a/b/c/d/abc.dat                
 /x/y/z/def.dat
 /p/q/r/s/t/test.dat
 /a/b/c/d/abc.dat                
 /x/y/z/def.dat

The output should be

 absolute path           relative path
 -------------           -------------
 /a/b/c/d/abc.dat        ABCD_PATH       
 /x/y/z/def.dat          XYZ_PATH   
 /p/q/r/s/t/test.dat     PQRST_PATH
 /a/b/c/d/abc.dat        ABCD_PATH       
 /x/y/z/def.dat          XYZ_PATH
  • If the 1st column contains /a/b/c/d, then in the 2nd column it should place some unique string which I give here as ABCD_PATH.

  • If the 1st column contains /x/y/z/, then in the 2nd column it should place some unique string which I give here as XYZ_PATH.

  • If the 1st column contains /p/q/r/s/t/, then in the 2nd column it should place some unique string which I give here as PQRST_PATH.

How to&Answers:
  1. Create a mapping table in a sheet in your excel file (sheet2 for example):

     ______________________________________
    |   |    A       |      B
    |___|____________|_____________________
    | 1 | Absolute   |   ReplaceBy
    |   |------------|---------------------
    | 2 |/a/b/c/d/   |   ABCD_PATH
    | 3 |/x/y/z/     |   XYZ_PATH
    | 4 |/p/q/r/s/t/ |   PQRST_PATH
    |
    |_________          ___________________
    \ sheet1 /\ sheet2 /\ sheet3 /
     \______/  \______/  \______/
    
  2. Back to sheet1:

     ___________________________________________________
    |   |    A               |      B
    |___|____________________|__________________________
    | 1 | absolute path      |   relative path
    |   |--------------------|--------------------------
    | 2 |/a/b/c/d/abc.dat    |   =VLOOKUP(LEFT(A2...    --(hit)-> Ctrl+Shift+Enter
    | 3 |/x/y/z/def.dat      |           |
    | 4 |/p/q/r/s/t/test.dat |           V (copy cell)
    |
    |         __________________________________________
    \ sheet1 /\ sheet2 /\ sheet3 /
     \______/  \______/  \______/
    
  3. Put in relative path B2 cell this formula:

    =VLOOKUP(LEFT(A2,
                  MAX((MID(A2, ROW($1:$90), 1)="/")*ROW($1:$90))
                 ),
             Sheet2!$A$2:$B$4,
             2,
             FALSE)
    
  4. Hit Ctrl+Shift+Enter : it’s an array formula, also known as a CSE formula.

  5. Copy B2 cell (not the formula) and paste it in the remaining cells in column B.

Answer:

How about

=TRIM(SUBSTITUTE(UPPER(LEFT(A1,FIND("~",SUBSTITUTE(A1,"/","~",LEN(A1)-LEN(SUBSTITUTE(A1,"/","")))))),"/",""))&"_PATH" for the path

and =RIGHT(A1,LEN(A1)-FIND("~",SUBSTITUTE(A1,"/","~",LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))))) for the file and extension