[VBA] Extract the folder names from a path

zeromax1

Board Regular
Joined
Mar 20, 2020
Messages
52
Office Version
  1. 365
Platform
  1. Windows
Dear all expert. I have a question about how to extract the different folder name from a path?

I use an VBA to get the file name & path from the servers.

For example:

C:\Users\user.abc\ABC Group\Accounting Folder - DOC Library\ABCDEFG LTD (abcd)\2020\PLS.xlsx
C:\Users\user.abc\ABC Group\Accounting Folder - DOC Library\HIJKLMN LIMITED (HIJK)\2020\PLS.xlsx
C:\Users\user.defg\ABC Group\Accounting Folder - DOC Library\Google LIMITED (GLG)\2021\PLS.xlsx
C:\Users\user.beg\ABC Group\Accounting Folder - DOC Library\Apple (APP)\2021\PLS.xlsx

May I know are there possible to extract the Company Name & Year to different columns?

Company NameYear
ABCDEFG LTD (abcd)2020
HIJKLMN LIMITED (HIJK)2020
Google LIMITED (GLG)2021
Apple (APP)2021

Many thanks for all of you if possible to advise any solution.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try this:

VBA Code:
Sub a1158542a()
Dim i As Long
Dim va, vb, x

va = Range("A1", Cells(Rows.Count, "A").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 2)

For i = 2 To UBound(va, 1)
    x = Split(va(i, 1), "\")
   
        If UBound(x) > 2 Then
            vb(i, 1) = x(UBound(x) - 2)
            vb(i, 2) = x(UBound(x) - 1)
        End If
       
Next

Range("B1").Resize(UBound(vb, 1), 2) = vb
End Sub


de Userform template-example-sample.xlsm
ABC
1data
2C:\Users\user.abc\ABC Group\Accounting Folder - DOC Library\ABCDEFG LTD (abcd)\2020\PLS.xlsxABCDEFG LTD (abcd)2020
3C:\Users\user.abc\ABC Group\Accounting Folder - DOC Library\HIJKLMN LIMITED (HIJK)\2020\PLS.xlsxHIJKLMN LIMITED (HIJK)2020
4C:\Users\user.defg\ABC Group\Accounting Folder - DOC Library\Google LIMITED (GLG)\2021\PLS.xlsxGoogle LIMITED (GLG)2021
5C:\Users\user.beg\ABC Group\Accounting Folder - DOC Library\Apple (APP)\2021\PLS.xlsxApple (APP)2021
Sheet3
 
Upvote 0
Hi Akuini, Thank you for your prompt reply. One more question about how to modified the code for longer path?


If my file path become longer than before like this:

C:\Users\user.abc\ABC Group\Accounting Folder - DOC Library\ABCDEFG LTD (abcd)\2020\PLS.xlsx
C:\Users\user.abc\ABC Group\Accounting Folder - DOC Library\ABCDEFG LTD (abcd)\PIC Name\2020\11-20\List.xlsx

The company name will extract 2020 instead of ABCDEFG LTD (abcd).

Try this:

VBA Code:
Sub a1158542a()
Dim i As Long
Dim va, vb, x

va = Range("A1", Cells(Rows.Count, "A").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 2)

For i = 2 To UBound(va, 1)
    x = Split(va(i, 1), "\")
  
        If UBound(x) > 2 Then
            vb(i, 1) = x(UBound(x) - 2)
            vb(i, 2) = x(UBound(x) - 1)
        End If
      
Next

Range("B1").Resize(UBound(vb, 1), 2) = vb
End Sub
 
Upvote 0
What the result should look like for this?
C:\Users\user.abc\ABC Group\Accounting Folder - DOC Library\ABCDEFG LTD (abcd)\PIC Name\2020\11-20\List.xlsx
 
Upvote 0
What the result should look like for this?
C:\Users\user.abc\ABC Group\Accounting Folder - DOC Library\ABCDEFG LTD (abcd)\PIC Name\2020\11-20\List.xlsx

It extract 2020 and 11-20. But I still want to extract the ABCDEFG LTD(abcd).
 
Upvote 0
It extract 2020 and 11-20. But I still want to extract the ABCDEFG LTD(abcd).
So "PIC Name" is ignored?
What criteria do you use to pick up "ABCDEFG LTD (abcd)" but not "PIC Name"?
 
Upvote 0
So "PIC Name" is ignored?
What criteria do you use to pick up "ABCDEFG LTD (abcd)" but not "PIC Name"?
The pattern for the below path is fixed:

Only the text with red colour will be changed base on the users and the company.

C:\Users\user.abc\ABC Group\Accounting Folder - DOC Library\Company Name\

Is it possible to only extract the company name? Then I can modified the code to extract the other text.
 
Upvote 0
Try this:
VBA Code:
Sub a1158542b()
Dim i As Long
Dim va, vb, x

va = Range("A1", Cells(Rows.Count, "A").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 1)

For i = 2 To UBound(va, 1)
    x = Split(va(i, 1), "\")
        If UBound(x) > 4 Then
            vb(i, 1) = x(5)
        End If
Next

Range("B1").Resize(UBound(vb, 1), 1) = vb
End Sub

Book1
AB
1data
2C:\Users\user.abc\ABC Group\Accounting Folder - DOC Library\ABCDEFG LTD (abcd)\2020\PLS.xlsxABCDEFG LTD (abcd)
3C:\Users\user.abc\ABC Group\Accounting Folder - DOC Library\HIJKLMN LIMITED (HIJK)\2020\PLS.xlsxHIJKLMN LIMITED (HIJK)
4C:\Users\user.defg\ABC Group\Accounting Folder - DOC Library\Google LIMITED (GLG)\2021\PLS.xlsxGoogle LIMITED (GLG)
5C:\Users\user.beg\ABC Group\Accounting Folder - DOC Library\Apple (APP)\2021\PLS.xlsxApple (APP)
6C:\Users\user.abc\ABC Group\Accounting Folder - DOC Library\ABCDEFG LTD (abcd)\PIC Name\2020\11-20\List.xlsxABCDEFG LTD (abcd)
Sheet5
 
Upvote 0
Excellent! Thank you very much.

Try this:
VBA Code:
Sub a1158542b()
Dim i As Long
Dim va, vb, x

va = Range("A1", Cells(Rows.Count, "A").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 1)

For i = 2 To UBound(va, 1)
    x = Split(va(i, 1), "\")
        If UBound(x) > 4 Then
            vb(i, 1) = x(5)
        End If
Next

Range("B1").Resize(UBound(vb, 1), 1) = vb
End Sub
 
Upvote 0
You're welcome, glad to help & thanks for the feedback. :)
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,592
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top