Consulting

Results 1 to 4 of 4

Thread: Excel VBA : How to break the file Path

  1. #1
    VBAX Regular
    Joined
    Feb 2017
    Location
    Hong Kong
    Posts
    33
    Location

    Excel VBA : How to break the file Path

    I have to choose the file and record the completely files name in excel cells. Have any coding to separate the "File name, Directory and sub-directory " and save it on worksheet ?

    such as :
    C:\Folder1\Folder2\Folder3\FileName1.jpg
    C:\Folder1\Folder2\Folder4\FileName2.jpg
    C:\Folder1\Folder2\Folder5\FileName3.jpg



    Target : (Dim A(),B(),C() as String)

    A = "C:\Folder1\Folder2"
    B = "Folder3", "Folder4" or "Folder5"
    C = "FileName1", "FileName2" or "FileName3"

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    This could use some error checking

    Option Explicit
    Sub test()
        Dim sPath As String, F1 As String, F2 As String, F As String
        Dim i As Long
        
        i = 1
        With ActiveSheet
            sPath = "C:\Folder1\Folder2\Folder3\FileName1.jpg"
            Call SplitPath(sPath, F1, F2, F)
            .Cells(i, 1) = sPath
            .Cells(i, 2) = F1
            .Cells(i, 3) = F2
            .Cells(i, 4) = F
            
            i = i + 1
     
            sPath = "C:\Folder1\Folder2\Folder4\FileName2.jpg"
            Call SplitPath(sPath, F1, F2, F)
            Call SplitPath(sPath, F1, F2, F)
            .Cells(i, 1) = sPath
            .Cells(i, 2) = F1
            .Cells(i, 3) = F2
            .Cells(i, 4) = F
            
            i = i + 1
            sPath = "C:\Folder1\Folder2\Folder5\FileName3.jpg"
            Call SplitPath(sPath, F1, F2, F)
            Call SplitPath(sPath, F1, F2, F)
            .Cells(i, 1) = sPath
            .Cells(i, 2) = F1
            .Cells(i, 3) = F2
            .Cells(i, 4) = F
            
            i = i + 1
        End With
    End Sub
    Sub SplitPath(P As String, Folder1 As String, Folder2 As String, FileName As String)
        Dim v As Variant
        
        v = Split(P, "\")
        
        FileName = v(UBound(v))
        Folder2 = v(UBound(v) - 1)
        
        ReDim Preserve v(LBound(v) To UBound(v) - 1)
        Folder1 = Join(v, "\")
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    Or did you mean something like this?
    Sub Or_Do_You_Mean_This()
    Dim c As Range, jve, j As Long
    For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    jve = Split(c, "\")
        For j = LBound(jve) To UBound(jve)
            c.Offset(, j + 1) = jve(j)
        Next j
    Next c
    End Sub

  4. #4
    VBAX Regular
    Joined
    Feb 2017
    Location
    Hong Kong
    Posts
    33
    Location
    Both is ok, thank you

Posting Permissions

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