Parsing part of file path

AChuckle

New Member
Joined
May 6, 2011
Messages
20
I am trying to parse out part of a file path where my excel file is stored using a VBA function.

My file path looks something like this: \\ServerName\GroupA\GroupB\2011\08-01\Name\B.Folder\Summary.xls

The function needs to keep only this part of the file path: \\ServerName\GroupA\GroupB\2011\08-01\Name\

Not really sure where to start on this problem. Seen a few ways to parse the file name from a file path but not a different section.

My intent is to change the end of the original file path from where my Summary.xls file is saved up one folder, over, and then down two more. Basically I want to take the parsed path the funtcion returns and add two more file locations down.

Anyone have a possible solution?

Thanks!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Code:
Function Parsename(r As String) As String
Dim fn As String, t, x As Long
fn = Mid(r, 2)
t = Split(fn, "\")
t(UBound(t) - 1) = "": t(UBound(t)) = ""
Parsename = "\" & Replace(Join(t, "\"), "\\", "\")
End Function

Sub test()
MsgBox Parsename("\\ServerName\GroupA\GroupB\2011\08-01\Name\B.Folder\Summary.xls")
End Sub
 
Upvote 0
Thanks for the replies. Very helpful. Tried them both out but the first was the one I could get working. When calling up the function HOTPEPPER posted I kept getting an error saying "Argument not Optional". The function is called after a macro button is pressed and the function itself was saved in a module. Not sure what that error means.
 
Upvote 0
Mine is written as a function not a sub, so you need to pass the pathname to it.

Notice the sub beneath the function, which calls the UDF.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top