VBA find folders in unknown subfolder whitout searching all subfolders

Ole Madsen

New Member
Joined
Feb 23, 2021
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
My question is as follows:
in the code below I am searching for a directory named Range ("D3") = #
and Range ("B3") = 21000 to find folder # 21000
The folder is located in D: \ Documents \? \ # 21000

My problem is that I have a lot of folders in D: \ Document with many sub folders so it takes a really long time to search.
my code searches all folders and sub folders through
Is there a way it only searches for folders with # in front and stops when found.

My code looks like this.

Sub Gem_Certifikat()

Dim searchFolderName As String
searchFolderName = "D:\Document"

Dim FileSystem As Object

Set FileSystem = CreateObject("Scripting.FileSystemObject")

doFolder FileSystem.GetFolder(searchFolderName)

End Sub

Sub doFolder(Folder)
Dim subFolder
Dim Get_path
Dim strCheckPath As String
Dim PDFfile As Range
For Each subFolder In Folder.SubFolders
If Split(subFolder, "\")(UBound(Split(subFolder, "\"))) = Range("D3") & Range("B3") = True Then


Get_path = subFolder
strCheckPath = subFolder
strCheckPath = subFolder & "\Certifikat" & "\" & Range("B5") & "\"
If Len(Dir(strCheckPath, vbDirectory)) = 0 Then MkDir strCheckPath


For Each PDFfile In ActiveSheet.Range("B6")
If PDFfile.Value <> "" Then
FileCopy Range("B7") & PDFfile.Value, strCheckPath & PDFfile.Value
End If
Next

MsgBox "Certifikat Gemt"
End

End If

doFolder subFolder

Next subFolder

Exit Sub

End Sub
 

Attachments

  • Screenshot_1.png
    Screenshot_1.png
    10 KB · Views: 27

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Welcome to MrExcel forums.

Instead of your recursive approach, which searches all levels of subfolders of "D:\Document", I would create an array containing the first-level subfolders in "D:\Document" (using FileSystemObject or the Dir function) and then use that array to look for the second-level subfolder named "# 21000".
 
Upvote 0
Maybe youjust need to add "s" to "Document"
searchFolderName = "D:\Documents"
Sub Gem_Certifikat()

Dim searchFolderName As String
searchFolderName = "D:\Documents"

Dim FileSystem As Object

Set FileSystem = CreateObject("Scripting.FileSystemObject")

doFolder FileSystem.GetFolder(searchFolderName)

End Sub

Sub doFolder(Folder)
Dim subFolder
Dim Get_path
Dim strCheckPath As String
Dim PDFfile As Range
For Each subFolder In Folder.SubFolders
If Split(subFolder, "\")(UBound(Split(subFolder, "\"))) = Range("D3") & Range("B3") = True Then


Get_path = subFolder
strCheckPath = subFolder
strCheckPath = subFolder & "\Certifikat" & "\" & Range("B5") & "\"
If Len(Dir(strCheckPath, vbDirectory)) = 0 Then MkDir strCheckPath


For Each PDFfile In ActiveSheet.Range("B6")
If PDFfile.Value <> "" Then
FileCopy Range("B7") & PDFfile.Value, strCheckPath & PDFfile.Value
End If
Next

MsgBox "Certifikat Gemt"
End

End If

doFolder subFolder

Next subFolder

Exit Sub

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,706
Members
449,048
Latest member
81jamesacct

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