vba string

ericlch16

Active Member
Joined
Nov 2, 2007
Messages
313
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
I have something like that

string1 = C:\mydocuments\text\something\anything

i want a quick function to get one level up i.e

string2 =C:\mydocuments\text\something

how to get string2 from string1?
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
ericlch16,

Try:


Code:
Option Explicit
Sub Test()
' hiker95, 04/13/2011
' http://www.mrexcel.com/forum/showthread.php?t=543437
Dim string1 As String, string2 As String
Dim Sp, s As Long
string1 = "C:\mydocuments\text\something\anything"
Sp = Split(string1, "\")
For s = LBound(Sp) To UBound(Sp) - 1
  string2 = string2 & Sp(s) & "\"
Next s
If Right(string2, 1) = "\" Then string2 = Left(string2, Len(string2) - 1)
MsgBox string2
End Sub
 
Upvote 0
any solution without the For loop?

ericlch16,

Try:


Code:
Option Explicit
Sub Test()
' hiker95, 04/13/2011
' http://www.mrexcel.com/forum/showthread.php?t=543437
Dim string1 As String, string2 As String
Dim Sp, s As Long
string1 = "C:\mydocuments\text\something\anything"
Sp = Split(string1, "\")
For s = LBound(Sp) To UBound(Sp) - 1
  string2 = string2 & Sp(s) & "\"
Next s
If Right(string2, 1) = "\" Then string2 = Left(string2, Len(string2) - 1)
MsgBox string2
End Sub
 
Upvote 0
Code:
Sub Foo()
    Debug.Print CreateObject("Scripting.FileSystemObject").GetParentFolderName("C:\SomeFolder\SomeFolder")
End Sub

Interestingly, it is not necessary for these folders to actually exist - it appears the function parses the file path in some manner no doubt very similar to Hiker's code.
 
Upvote 0
Hello,

There's a few other ways to do this without looping, probably more, e.g.,

Code:
Sub foo()
Dim strArr() As String
Let strArr = Split("C:\Temp\Gooney goo goo", "\")
ReDim Preserve strArr(UBound(strArr) - 1)
MsgBox Join$(strArr, "\")
End Sub
 
Sub bar()
Const StringVar As String = "C:\Temp\Gooney goo goo"
MsgBox Left$(StringVar, Len(StringVar) - _
    WorksheetFunction.Find("\", StrReverse(StringVar)))
End Sub

But I don't fully follow why you'd be so sure to walk away from loops? They can be quite fast in well-constructed code. I guarantee the above is iterative, under-the-hood, eh.
 
Upvote 0
How about:

Code:
Function UpOneLevel(sFilepath As String) As String

UpOneLevel = Left$(sFilepath, InStrRev(sFilepath, "\", IIf(Right$(sFilepath, 1) = "\", Len(sFilepath) - 1, -1)))

End Function
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,901
Members
452,948
Latest member
Dupuhini

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