Private Sub Command1_Click()
MsgBox GETDIR("c:\aaa\aaaa\aaaaa", 2)
End Sub
Function GETDIR(ByVal PATH As String, ByVal LEVEL As Integer) As String
Dim d() As String, num As Long, i As Integer
num = Len(PATH) - Len(Replace(PATH, "\", "")) + 1
If LEVEL > num Then MsgBox "Too much directories!": Exit Function
Do While num > LEVEL
PATH = Left(PATH, InStrRev(PATH, "\") - 1)
Debug.Print PATH
num = num - 1
Loop
GETDIR = PATH & "\"
End Function
Private Sub Command1_Click()
MsgBox GETDIR("c:\aaa\aaaa\aaaaa", 2)
End Sub
Function GETDIR(ByVal PATH As String, ByVal LEVEL As Integer) As String
Dim d() As String, i As Long
d = Split(PATH, "\")
i = UBound(d)
ReDim Preserve d(LEVEL - 1)
GETDIR = Join(d, "\") & "\"
Erase d
End Function