VBA function to read a Cell value from Right to left until the first '\' is found

NessPJ

Active Member
Joined
May 10, 2011
Messages
416
Office Version
  1. 365
Hello all,

Is there some way i could create a function to read a cell value from right to left until the first '\' is found.

So for example if i have a cell value that states: G:\application\log\log220620.txt
the function will only return "log220620.txt" ?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
InStrRev will find the position of a character searching from right to left, though the number returned is the position from left to right, so you can then use that with Mid$ to get the last part of the text - basically Mid$(text, instrrev(text, "\")+1). You could also use Split to convert the text to an array, and then just return the last item:

Code:
Dim parts
parts = Split(text, "\")
msgbox parts(ubound(parts))
 
Upvote 0
InStrRev will find the position of a character searching from right to left, though the number returned is the position from left to right, so you can then use that with Mid$ to get the last part of the text - basically Mid$(text, instrrev(text, "\")+1). You could also use Split to convert the text to an array, and then just return the last

Thanks! I did not know the Instrrev and MID function could work together this easily. This works fine! :)

Not to be a nag, but is it simple to also create a function that would return everything before the last '\' as well ?
 
Upvote 0
Yes, Just use Left$ instead of Mid$ and subtract 1 from the result of InstrRev instead of adding 1.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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