Searching for a folder within a folder (Excel- VBA)

B.C

I am given a bridge plan number which is a 4 digit number. I need to find a folder corresponding that bridge. The folder name will carry the bridge plan number and some other random text (eg "1234- washington street"). I've written some code that is able to achieve this but it is very slow. I was wondering if someone can com up with a more efficient way of doing it. Thanks.

Public FSO As New FileSystemObject

Public Function FoundPlan(bridge_plan As String)

    Dim objFolder As Folder
    Dim planFolder As Folder
    Dim Path As String
    Dim i As String


    Path = "G:\some\path"
    'This is the directory path that carries my list of folders

    Set objFolder = FSO.GetFolder(Path)

    If Not Len(bridge_plan) = 4 Then
        FoundPlan = ""
        Exit Function
    End If
    'If the given plan number is anything except 4 digits, the function returns 
    'nothing and exits

    For Each planFolder In objFolder.SubFolders

        If Not InStr(planFolder, bridge_plan) = 0 Then
            FoundPlan = Path & planFolder
            Exit For
        End If

    Next planFolder

    'For each subfolder in my directory I use instr to search for my number 
    'inside the folder path.

End Function
Tom

Try this instead. It doesn't need to loop through each folder

Public Function FoundPlan(bridge_plan As String) As String
    Dim planFolder As String, Path As String

    Path = "G:\some\path\"
    'This is the directory path that carries my list of folders

    If Not Len(bridge_plan) = 4 Then Exit Function

    planFolder = Dir(Path & bridge_plan & "*", vbDirectory)

    If Not planFolder = vbNullString Then FoundPlan = Path & planFolder
End Function

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

forbid saving as in a given folder using excel VBA

From Dev

How to create a folder within a folder in sysfs

From Dev

running perl scripts on folder within folder

From Dev

preg matching searching for file in folder

From Dev

Excel VBA open folder and get GPS info (Exif) of each files in it

From Dev

Excel VBA, set an Outlook Folder, which is a public folder

From Dev

Not searching for view in Areas folder

From Dev

VBA Excel prompt user to select a file in a default folder

From Dev

Get list of Excel files in a folder using VBA

From Dev

Excel VBA to save to specific folder (with current code generating filename)

From Dev

Upload Excel document to shared folder on Google Drive with Excel VBA?

From Dev

How to copy different versions of the same folder in excel vba?

From Dev

Check if folder exists and save two sheets in there VBA Excel

From Dev

VBA code to open all excel files in a folder

From Dev

Excel VBA - Multiple Dir() in Same Folder

From Dev

Ignore containing folder, but not a folder within

From Dev

Searching and Retriving image from a folder

From Dev

excel VBA ignore specific folder/file in directory

From Dev

preg matching searching for file in folder

From Dev

Path not found when creating folder in Excel VBA

From Dev

Excel VBA that loops through the folder and move data to the next column

From Dev

path of folder within a folder in terminal

From Dev

EXCEL VBA get source folder to Const

From Dev

Delete non-Excel Files in a folder VBA

From Dev

Searching for files within specific folder

From Dev

remove files then folder in excel 2010 vba

From Dev

Regex for VBA Excel macro for new folder chars

From Dev

Searching for public folder in routes path

From Dev

Giving a user permission to a folder within the root folder

Related Related

  1. 1

    forbid saving as in a given folder using excel VBA

  2. 2

    How to create a folder within a folder in sysfs

  3. 3

    running perl scripts on folder within folder

  4. 4

    preg matching searching for file in folder

  5. 5

    Excel VBA open folder and get GPS info (Exif) of each files in it

  6. 6

    Excel VBA, set an Outlook Folder, which is a public folder

  7. 7

    Not searching for view in Areas folder

  8. 8

    VBA Excel prompt user to select a file in a default folder

  9. 9

    Get list of Excel files in a folder using VBA

  10. 10

    Excel VBA to save to specific folder (with current code generating filename)

  11. 11

    Upload Excel document to shared folder on Google Drive with Excel VBA?

  12. 12

    How to copy different versions of the same folder in excel vba?

  13. 13

    Check if folder exists and save two sheets in there VBA Excel

  14. 14

    VBA code to open all excel files in a folder

  15. 15

    Excel VBA - Multiple Dir() in Same Folder

  16. 16

    Ignore containing folder, but not a folder within

  17. 17

    Searching and Retriving image from a folder

  18. 18

    excel VBA ignore specific folder/file in directory

  19. 19

    preg matching searching for file in folder

  20. 20

    Path not found when creating folder in Excel VBA

  21. 21

    Excel VBA that loops through the folder and move data to the next column

  22. 22

    path of folder within a folder in terminal

  23. 23

    EXCEL VBA get source folder to Const

  24. 24

    Delete non-Excel Files in a folder VBA

  25. 25

    Searching for files within specific folder

  26. 26

    remove files then folder in excel 2010 vba

  27. 27

    Regex for VBA Excel macro for new folder chars

  28. 28

    Searching for public folder in routes path

  29. 29

    Giving a user permission to a folder within the root folder

HotTag

Archive