[VBA] Extract string after last "\" in string.

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
807
Office Version
  1. 365
Platform
  1. Windows
Hi guys,

I have a folder path and I'm just trying to extract the string after the last "\" in the string. I remember this needs something like Split & InStrRev but I can't quite remember how to do it as it's been a couple years since playing with VBA.

When I do something like
VBA Code:
WeekData = InStrRev(MyFolder, "\")

it gives me the position of the last occurrence of the slash, in this instance it's position 106.

Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
One way...

Arr = Split(MyFolder, "\")
WeekData = Arr(Ubound(Arr))
 
Upvote 0
Solution
If you wanted to do it using InStrRev...

WeekData = Mid(MyFolder, InStrRev(MyFolder, "\") + 1)
 
Upvote 0
One way...

Arr = Split(MyFolder, "\")
WeekData = Arr(Ubound(Arr))
Unfortunately this comes up with ""

Here's my example string, just so we can work off the same page.

"C:\Users\Me\MyCompany\Reporting - Documents\Supplier Unbilled\2023-06\28-06-2023\"

What I'd like to be able to do is take the first two characters of the string using Left,2

So if the string was "15-04-2023 Report.csv" after the last slash, I just want the "15"

Ah ****, no I see the problem now... Ok all I need is that "28" lol.


Sorry, when you have a long string it doesn't show it all in the preview.. That stopped me seeing the last slash. I only printed it so I could grab it to show you.
 
Upvote 0
Unfortunately this comes up with ""

Here's my example string, just so we can work off the same page.

"C:\Users\Me\MyCompany\Reporting - Documents\Supplier Unbilled\2023-06\28-06-2023\"

What I'd like to be able to do is take the first two characters of the string using Left,2

So if the string was "15-04-2023 Report.csv" after the last slash, I just want the "15"

Ah ****, no I see the problem now... Ok all I need is that "28" lol.


Sorry, when you have a long string it doesn't show it all in the preview.. That stopped me seeing the last slash. I only printed it so I could grab it to show you.
All sorted

VBA Code:
Arr = Split(MyFolder, "\")
WeekData = WorksheetFunction.RoundUp(Left(Arr(UBound(Arr) - 1), 2) / 7, 0)

I'm taking the report date and figuring out which week in the month it belongs to :)

Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,530
Messages
6,179,373
Members
452,907
Latest member
Roland Deschain

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