Excel formula to extract file name from paths without file extension - excel

I have this excel formula which I'm hoping can be amended to extract the file name without the file extension. I know this can be done over two formulas but it would make the process I am working with a whole lot easier. I'm also trying to avoid VBA as I don't have any knowledge on it.
=IFERROR(MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"\",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,LEN(A1)),"")
Thanks!

You already have logic for finding the position of the last "\" character in string. You can use the same logic for finding the position of the last "." character in string. Then the difference of those two numbers minus one will be the length of the file name without the file extension. Use this length for the num_chars argument ot the MID function. For example...
=IFERROR(MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"\",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,FIND(CHAR(1),SUBSTITUTE(A1,".",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,".",""))))-FIND(CHAR(1),SUBSTITUTE(A1,"\",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))-1),"")
Note that this formula assumes that all file names have an extension. If some files have no extension then you would need to add extra logic to the formula.

It looks like you expect A1 to contain the filename, but you can do this all at once if you're willing to replace every instance of "A1" with Cell("filename",A1). Either way, it looks like you are overcomplicating the situation by looking to count the number of file paths, when really all you need is the "[" & "]" which encloses the name of the file.
If A1 is any random cell with nothing special in it, the formula looks like this:
=MID(CELL("filename",A1),FIND("[",CELL("filename",A1))+1,FIND("]",CELL("filename",A1))-FIND("[",CELL("filename",A1))-1)
If A1 holds CELL("filename",B1), then the formula looks like this:
=MID(A1,FIND("[",A1)+1,FIND("]",A1)-FIND("[",A1)-1)
CELL("filename",B1) looks at B1, and pulls the property "filename". In this case, it doesn't matter whether you use B1, Z10, etc. - it just can't be a circular reference. You can also use CELL to pull cell-specific info, such as cell address etc.
Then the MID function simply looks at A1, starts 1 after the "[", and picks up all the characters up to the "]".

Related

splitting underscores in Excel

I'm fairly new to Excel and need some assistance. I have a Column that has a list of files that look like:
12345_v1.0_TEST_Name [12345]_01.01.2022.html
45321_v55.9_Some Name Here [64398]_07.15.2018.html
56871_v14.2_Test[64398]_10.30.2019.html
Each file name can be different depending on what output is provided to me.
Note: There are other random files in the same format, however where it says Test_Name there could be an underscore and sometimes no underscore. Would like that to be ignored in the formula or vba. Files also can change but will be in the same format.
I need some help with a formula or vba that splits the underscores and outputs the data into their own cells:
Column C 12345
Column D v1.0
Column E TEST_Name [12345]
Column F 01.01.2022
Column G .html
Since there can be different file extensions however the format remains same, hence the above formula which i provided has been amended with some few tweaks so that it works for any file extensions,
FORMULA IN CELL C1
=IF(LEN($B1)-LEN(SUBSTITUTE($B1,"_",""))+1>4,
TRIM(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($B1,"."&TRIM(RIGHT(SUBSTITUTE(
SUBSTITUTE($B1,"."," ",LEN($B1)-LEN(SUBSTITUTE($B1,".","")))," ",REPT(" ",200)),100)),"_"&"."&
TRIM(RIGHT(SUBSTITUTE(SUBSTITUTE($B1,"."," ",LEN($B1)-LEN(SUBSTITUTE($B1,".","")))," ",
REPT(" ",200)),100))),"_"," ",3),"_",REPT(" ",100)),COLUMN(A1)*99-98,100)),
TRIM(MID(SUBSTITUTE(SUBSTITUTE($B1,"."&TRIM(RIGHT(SUBSTITUTE(SUBSTITUTE(
$B1,"."," ",LEN($B1)-LEN(SUBSTITUTE($B1,".","")))," ",REPT(" ",200)),100)),"_"&"."&
TRIM(RIGHT(SUBSTITUTE(SUBSTITUTE($B1,"."," ",LEN($B1)-LEN(SUBSTITUTE($B1,".","")))," ",
REPT(" ",200)),100))),"_",REPT(" ",100)),COLUMN(A1)*99-98,100)))
FILL DOWN & FILL ACROSS!!!
There are other random files in the same format.....Files also can change but will be in the same format.
So, assuming the files indeed will be in the same format, we can brake this query down into the following requirements:
Change the 1st and 2nd occurence and the very last of the underscore into anything to split on;
Change the dot before the file-extension into anything to split on under the assumption we don't know if this would be '.html' or any other extension.
Since you have Microsoft365 we can use dynamic arrays and some basic functions to retrieve what you want:
=LET(X,SEARCH("_??.??.????.",A1),Y,"</s><s>",TRANSPOSE(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(REPLACE(A1,X,12,Y&MID(A1,X+1,10)&Y),"_",Y,2),"_",Y,1)&"</s></t>","//s")))
To break this down a little bit:
SEARCH("_??.??.????.",A1) - This part will make sure that we find the position of the very last underscore upto the dot before the file extension assuming you don't have any other date in your filenames in this specific format;
SUBSTITUTE() - We can use this formula to specifically change the 1st and 2nd instances of the underscore to anything we can split on;
FILTERXML() - You may notice we used valid xml start/end-tags to split our data using this function.
TRANSPOSE() - This last function will now spill the returned array over the columns instead of rows.
Without LET():
=TRANSPOSE(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(REPLACE(A1,SEARCH("_??.??.????.",A1),12,"</s><s>"&MID(A1,SEARCH("_??.??.????.",A1)+1,10)&"</s><s>"),"_","</s><s>",2),"_","</s><s>",1)&"</s></t>","//s"))
Is this what you are trying to achieve, although there might be more eloquent way to use a formula, and solve this, however you may try using this as well,
FORMULA USED IN CELL C1
=IF(LEN($B2)-LEN(SUBSTITUTE($B2,"_",""))+1>4,TRIM(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($B2,".html","_.html"),"_"," ",3),"_",REPT(" ",100)),COLUMN(A2)*99-98,100)),TRIM(MID(SUBSTITUTE(SUBSTITUTE($B2,".html","_.html"),"_",REPT(" ",100)),COLUMN(A2)*99-98,100)))
Fill Down & Fill Across !

Unable to convert text to Numbers

I've been given an excel to import on Database, it was exported from an Access DB. in the excel there's a column type_class, in one excel it's good(sheet1), but on another excel which I moved to sheet2 to make VLOOKUP function, I can't tell whether it's a text or a number column from the first sight. the upper-left green-thing is not showing on all cells. but, using ISTEXT function result in text. below is the original column without any changes or formatting, as well as ISTEXT result.
when I use the column in a VLOOKUB function to transfer the Name to the first sheet, only (1010, 1101, 1102,....), hence the cells with the green-mark on the upper-left corner.
I can easly format the key in sheet1 using text-to-columns, cell formatting, and any other way.
but I cannot change the column in sheet2, I tried:
Text-to-Columns
Cell Formatting
VALUE(text), CLEAN(text), TRIM(text), TRIM(CLEAN(text)), CLEAN(SUBSTITUTE())
Multiply by 1
but only the cell with the green-mark changes to a number, the rest stays the same. I browsed the internet but didn't get a solution either.
Edit:
I uploaded what is need to test the case on the drive. you can find it here
Help Appreciated
For your digit strings that you can't convert to text, from the comments it seems there are extra characters in that string not removable by TRIM or CLEAN.
Determine what those character are
Assume a "non-convertible" digit string is in A1
Enter the following formula
B1: =MID($A$1,ROWS($1:1),1) and fill down
C1: = UNICODE(B1) and fill down
From this you can determine the character to use in a SUBSTITUTE function.
For example:
From the above we see that the character code that we need to get rid of is 160.
So we use:
=SUBSTITUTE(A1,CHAR(160),"")
or, to convert it in one step to a number:
=--SUBSTITUTE(A1,CHAR(160),"")
Note If the character code is >255, use UNICHAR instead of CHAR in the SUBSTITUTE function.
Without an example, I use value() to convert what excel takes as text like so:
=value(left(“10kg”,2))
Or the following also works:
=left(“10kg”,2)*1
Note those double quotes should be the straight ones - sorry smartphone is not always smart...
And if leading or trailing spaces are an issue, then trim() is one solution.

Excel Dynamic Hyperkink using Cell Value as part of Address

I have a column of part numbers which I would like to associate with a column of links to their corresponding autocad drawings. The drawings are on a server, I would like to be able to insert the part number into the file path.
My attempt:
=HYPERLINK(\\servername\folderofparts\partprefix\['Lists for Calculations'!B8].dwg","Link")
where cell B8 within the sheet Lists for Calculations contains the name of the part.
try,
=HYPERLINK("\\servername\folderofparts\partprefix\" & 'Lists for Calculations'!B8 & ".dwg", "Link")
tbh, it's a little unclear whether the square brackets were intended to be part of the filename or not. I removed them above.

Concatenating File Path Excel

Lets say I have columns with dates on them.
The file path is something like this:
='U:\Report\[Date 042516.xls]Joe Smoe'!$C81
Where the '042516' is variable to each column. !$C81 is variable to each row in the original document.
How do I do this and pull up the respective numbers from the reports and dates?
I think I understand - so you have a column with numbers (042516,042517,042518, etc), and you want the formula to update that part of the path, as you drag down?
If your 042516 is in C1, you can use this:
=INDIRECT("'U:\Report\[Date "&C1&".xls]Joe Smoe'!$C81")
(Note the workbook being referenced, Date 042516.xls, must be open for Indirect to work.)

What Excel formula returns the sheet name?

I have searched the excel function documentation and general MSDN search but have been unable to find a way to return the sheet name without VBA.
Is there a way to get the sheet name in an excel formula without needing to resort to VBA?
Not very good with excel, but I found these here
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,256)
and A1 can be any non-error cell in the sheet.
For the full path and name of the sheet, use
=CELL("filename",A1)
Here's a reasonably short one that has a couple added benefits:
Does a reverse lookup (most other answers go wrong direction) by using the often ignored REPT function.
A1 seems like a poor choice as there's a considerably higher chance it errors compared to... $FZZ$999999.
Don't forget to absolute. Copying and pasting some of the other examples could error due to referential changes.
The ? is intentional as that shouldn't be in the file path.
=SUBSTITUTE(RIGHT(SUBSTITUTE(CELL("Filename",$FZZ$999999),
"]",REPT("?", 999)), 999),"?","")
The below will isolate the sheet name:
=RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("]",CELL("filename")))
For recent versions of Excel, the formula syntax is:
=MID(CELL("filename";A1);FIND("]";CELL("filename";A1))+1;255)
None of the formulas given in other answers supports the case if there is character ] in filepath.
The formula below is more complex, but it can handle such cases properly:
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1),FIND("?",SUBSTITUTE(CELL("filename",A1),"\","?",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\","")))))+1,LEN(CELL("filename",A1)))
I had a module already open so I made a custom function:
Public Function Sheetname (ByRef acell as Range) as string
Sheetname = acell.Parent.Name
End Function
Had a square bracket ']' in the file name, so needed to modify the above formula as per the following to find the last occurrence of the square bracket.
Verified that this works for 0,1 or more square brackets in the file name / path.
=RIGHT(CELL("filename"),LEN(CELL("filename")) - FIND("]]]",SUBSTITUTE(CELL("filename"),"]","]]]",LEN(CELL("filename"))-LEN(SUBSTITUTE(CELL("filename"),"]","")))))
Replaces all square brackets using the substitute function, then compares the length of the result with the length of the file name to identify the number of square brackets in the file name.
Uses the number of occurrences of square brackets to substitute the last square bracket with 3 sequential square brackets ']]]'
Uses the Find function to identify the location of the 3 square brackets in the string
Subtracts the location of the 3 square brackets from the length of the full path
Uses the result to get the right most characters (being the sheet name)
Previous comment above about saving the workbook first is also a key, as you'll otherwise receive the #Value! result.
The below works for me and is simpler (to me at least) than other solutions, while still handling a square bracket in the file name:
=MID(CELL("filename", A1),6+SEARCH(".xlsx]",CELL("filename", A1)),32)
This assumes it is an "xlsx" file and that the filename will not contain ".xlsx]" in addition to the xlsx suffix, which in my case is an assumption that is safe enough to make.
If you wanted to handle both ".xlsx" and ".xls" file names, you could use:
=MID(SUBSTITUTE(CELL("filename", A1),".xls]",".xlsx]"),6+SEARCH(".xlsx]",SUBSTITUTE(CELL("filename", A1),".xls]",".xlsx]")),32)
I'm pretty sure you could have Googled this. I just did, and here is the very first thing that came up for me.
In Excel it is possible to use the CELL function/formula and the MID and FIND to return the name of an Excel Worksheet in a Workbook. The formula below shows us how;
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,256)
Where A1 is any non error cell on the Worksheet. If you want the full path of the Excel Workbook, simply use;
=CELL("filename",A1)
The only catch is that you have to save the file for this to work!

Resources