Trimming varaible text strings using VBA

Mc Culloch

Board Regular
Joined
Jul 21, 2005
Messages
68
Got a problem that I can't work out how to do using the regular TEXT functions.

I have a text string which represents a files path name [returned using "Application.GetOpenFileName()"]. For example,

C:\WorkFiles\2005_06\Nov\Reports.xls

I need to be able to trim off the file name, leaving the full path down to the folder. The example above would be as follows,

C:\WorkFiles\2005_06\Nov\

The logic I was thinking of wouyld be to trim off all the chars from right to left until you reach the first \ but I can't get my head round how you would do that using the standard functions.

Any ideas gratefully received!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
There is probably a better way using XL native Functions rather than the InStrRev VBA one but here you go:

Code:
Sub Test()
    strPath = "C:\FolderMain\Next\Book.xls"
    MsgBox Left(strPath, InStrRev(strPath, "\"))
End Sub

Regards.
 
Upvote 0
rafaaj2000 said:
There is probably a better way using XL native Functions rather than the InStrRev VBA one but here you go:

Code:
Sub Test()
    strPath = "C:\FolderMain\Next\Book.xls"
    MsgBox Left(strPath, InStrRev(strPath, "\"))
End Sub

Regards.

Hi Jafaar,

Sorry to be thick, how would you do this is you didn't want the file path but just the file name?

Thanks,

Paul
 
Upvote 0
You can try this :

Code:
Sub Test()

   strPath = "C:\FolderMain\Next\Book.xls"
   MsgBox Right(strPath, Len(strPath) - InStrRev(strPath, "\"))
    
End Sub

Regards.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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