I m new in VBA and below is my code which is not working, can any one of u can help?
Dim nPath1() As String
nPath1() = Split(nPath, "\")
'Declare path as integer
Dim path As Integer
'Getting array length
path = UBound(nPath1())
Dim lastname As String
'For Loop
For i = 0 To path-1
lastname += nPath1(i)+"\"
Next i
The above code is not working; my path string is Root\zTrash - No longer needed\NOC\NOC and what I want is Root\zTrash - No longer needed\NOC.
If you are fan of long formulas, this is another option:
\
If you want to remove just the last item from your path, you can do it this way:
InStrRev
finds the position of the last occurrence of\
Left
truncates the string until that positionThe
-1
is because you want also to remove that last\
This
gives you full nPath1 array. If you want to skip last element (and I'm not sure what you exactly want), you should use path-2
Or you can try:
This is useful if you want to remove more than one item (not just the last one) by changing
1
to 2 or 3 for example:Results: