PDA

View Full Version : Solved: How do you remove all text after the last \ in a string



k13r4n
01-07-2009, 05:58 PM
So i have the filname and location in a string and i want to chop the filename off so i just have the folder.

E.G. "S:\CompanyServer\Mydepartment\MyWork\ScriptFile.scr" becomes S:\CompanyServer\Mydepartment\MyWork

i cant just use Left(Filename, #) because the length of the string depends were the user saves the file and what filename they use.

any ideas?
Ive searched for an answer to this one on and off for a while and can't find anything.

Cheers


Kieran :thumb

Edit: Forgot to mention this is for setting ChDir (), as for 'why', its a very long explination and is more of a "it would be nice if it did this.." request from one of the people that use the spreadsheet. Im not a VBA guru or anything. Someone at work mentioned to the boss that i did a bit of web development on the side and so i got landed with writing some VBA to Automate a few things, im pretty much making it up as i go along *groan*

Kenneth Hobs
01-07-2009, 06:10 PM
Sub test()
MsgBox FolderName(ThisWorkbook.FullName)
End Sub

Function FolderName(fullPathfilename As String)
FolderName = Left(fullPathfilename, InStrRev(fullPathfilename, "\"))
End Function

k13r4n
01-07-2009, 06:49 PM
your a legend :bow:

thankyou :thumb