PDA

View Full Version : [SOLVED:] Declaring a variable using part of a filepath name



branston
02-27-2020, 06:29 AM
Hi

I hope this is an easy question for someone. I am pointing a variable to the "end" part of another filepath ie. the last sub folder name. My filepaths can change and because I am using 'P1' elsewhere in my code I'd rather not change P1 each time. I need P1 to be 'relative'



Dim dA$, P1, strDir$, strDir2$

dA = "C:\temp\Users\Alan\Section2Note1Appendix45\"
P1 = "Section2Note1Appendix45" ???????? (need help here)

strDir = Dir(dA) ?????????
strDir2 = Right(strDir, 10) ???????? (need help here)



P1 needs to be the name of the last folder in dA each time. I need a bit of help with strDir2 above / or a better way. Many thanks in advance.

JKwan
02-27-2020, 08:19 AM
something like this:


Sub test()
Dim dA$, P1, strDir$, strDir2$
Dim temp

dA = "C:\temp\Users\Alan\Section2Note1Appendix45\"

temp = Split(dA, "\")
P1 = temp(UBound(temp) - 1)
MsgBox P1
End Sub

Paul_Hossler
02-27-2020, 08:41 AM
Maybe


Option Explicit


Sub Test()
Dim dA$, P1, strDir$, strDir2$
Dim v As Variant


dA = "C:\temp\Users\Alan\Section2Note1Appendix45\"
v = Split(dA, Application.PathSeparator)

If Len(v(UBound(v))) = 0 Then
P1 = v(UBound(v) - 1)
Else
P1 = v(UBound(v))
End If

MsgBox P1


'
'P1 = "Section2Note1Appendix45" ???????? (need help here)
'
'strDir = Dir(dA)
'strDir2 = Right(strDir, 10)
End Sub

branston
02-27-2020, 08:49 AM
Thanks guys - will try both methods out.