Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-13-2024, 09:41 AM
RobiNew RobiNew is offline Get the whole path before the last counterslash Windows 10 Get the whole path before the last counterslash Office 2016
Competent Performer
Get the whole path before the last counterslash
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default Get the whole path before the last counterslash

I know how to get the last subfolder in a path (here below), but how do I get the whole path before the last counterslash? Thanks!
Code:
Sub BeforeCounterslash()
Dim StrText As String
StrText = Trim(Split("C:\Sub_1\Sub_2\Sub_3", "\")(3))
MsgBox StrText
End Sub

Reply With Quote
  #2  
Old 01-13-2024, 10:06 AM
RobiNew RobiNew is offline Get the whole path before the last counterslash Windows 10 Get the whole path before the last counterslash Office 2016
Competent Performer
Get the whole path before the last counterslash
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

This one works OK, but is there a simpler method? Thanks!
Code:
Sub TestNew()
Dim StrPath As String, t As Long
StrPath = "C:\Sub_1\Sub_2\Sub_3"
If Right(StrPath, 1) = "\" Then StrPath = Left(StrPath, Len(StrPath) - 1)
t = InStrRev(StrPath, "\")
StrPath = Left(StrPath, t - 1)
MsgBox StrPath
End Sub
Reply With Quote
  #3  
Old 01-13-2024, 11:30 AM
vivka vivka is offline Get the whole path before the last counterslash Windows 7 64bit Get the whole path before the last counterslash Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! I would use a simplified variant of your code without any Ifs:
Code:
Sub BeforeCounterslash()

Dim StrPath As String
StrPath = "C:\Sub_1\Sub_2\Sub_3"

  StrPath = left(StrPath, InStrRev(StrPath, "\") - 1)
MsgBox StrPath
End Sub
Reply With Quote
  #4  
Old 01-13-2024, 03:35 PM
RobiNew RobiNew is offline Get the whole path before the last counterslash Windows 10 Get the whole path before the last counterslash Office 2016
Competent Performer
Get the whole path before the last counterslash
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks a lot, Vivka! You are always the best!
Reply With Quote
  #5  
Old 01-13-2024, 03:49 PM
vivka vivka is offline Get the whole path before the last counterslash Windows 7 64bit Get the whole path before the last counterslash Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Thank you, RobiNew, for your kind words! I'm not the best, I'm still learning. There are real gurus here. Besides, it may be a revelation to you, I've learned some new tricks from your codes, too. So, you are also a kind of my teacher.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the whole path before the last counterslash PDF default path hernans Word VBA 8 07-02-2018 06:06 PM
Critical path bijukn1 Project 2 11-04-2016 10:31 AM
Get the whole path before the last counterslash Error with UNC path Troy R Mail Merge 1 11-11-2015 09:06 AM
Get the whole path before the last counterslash Change old path to new path (batch) NobodysPerfect Word VBA 2 08-14-2014 10:09 PM
Doc path cinque8 Word 4 08-10-2012 06:57 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:51 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft