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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

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