How do I set a cell's value to the parent of the directory the file is in?
I've got a tedious and repetitious task ahead of me, and I'm trying to automate as much of this as possible—but VBA isn't allowed.
I'm going to have a directory tree of Excel spreadsheets, looking something like this:
C:\…\Parent1\Dir2\File3.xlsx
…\Parent4\Dir5\File6.xlsx
…
I need a formula which will extract the parent directory, eg , Parent1
or Parent4
in this example.
[ Note : The file and directory names do not follow any particular convention; suggestions to look for numbers will match this anonymized example but won't help me. ]
Notes:
I have found elsewhere on the 'net the following formulae, which I've needed as well and which might point to the solution.
The current sheet name:
= RIGHT(CELL("filename",A1), LEN(CELL("filename",A1)) - FIND("]", CELL("filename",A1)))
The current file name:
= MID(CELL("filename",A1), FIND("[", CELL("filename",A1)) + 1, FIND(".", CELL("filename",A1)) - (FIND("[", CELL("filename",A1)) + 1))
The current directory name (from MrExcel.com ):
= TRIM(RIGHT(SUBSTITUTE(LEFT(CELL("filename",A1), FIND("[", CELL("filename",A1), 1) - 2), "\\", REPT(" ",100)), 100))
The poster explained his method thus:
It works by getting the DIR and replacing all "\\" with 100 spaces. It then grabs the right 100 chars which will be a bunch of spaces and the final DIR name then it trims off the preceding spaces.
This last feels like it should be adaptable to extracting the parent directory, but I haven't figured out how to write this adaptation.
Try this. It goes unsaid that this formula will only work in a saved
file.
=TRIM(LEFT(RIGHT(SUBSTITUTE(LEFT(CELL("filename",A1), FIND("[", CELL("filename",A1), 1) - 2),"\\",REPT(" ",100)),200),100))
Screenshot :
LOGIC :
To find a string between two characters, use this. (Finding between say "\\")
=TRIM(LEFT(RIGHT(SUBSTITUTE(A1,"\",REPT(" ",100)),200),100))
The below gives the full path
=LEFT(CELL("filename",A1), FIND("[", CELL("filename",A1), 1) - 2)
For example: C:\\Temp\\Folder 1
So just insert the second one in the first formula
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.