Consulting

Results 1 to 4 of 4

Thread: Declaring a variable using part of a filepath name

  1. #1

    Declaring a variable using part of a filepath name

    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.
    Last edited by branston; 02-27-2020 at 07:47 AM.

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    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

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Thanks guys - will try both methods out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •