+ Reply to Thread
Results 1 to 6 of 6

Can you control which order Subfolders are searched?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-17-2017
    Location
    Alberta, Canada
    MS-Off Ver
    365 MSO Version 2407 Build 16.0.17830.20166
    Posts
    133

    Can you control which order Subfolders are searched?

    My current code is working almost perfectly. The only problem is that it doesn't find all the instances of the files existence.

    The code should search folders for items in column A, add a unique number and store it in that cell.

    Example:
    If 04062017003-1 is in column A
    And 04062017003-1, 04062017003-2, 04062017003-3
    all exist in the folders being searched, it should change the cell contents to 04062017003-4

    I believe that if the search was done from the first subfolder to the last then the desired results would be produced. (In the example: 04062017003-3 could never exist prior to 04062017003-2 or 04062017003-1)

    Folders are arranged in this manner
    ...2017
    ...2017\2017-01
    ...2017\2017-01\2017-01-01
    2017-01-02
    2017-01-03
    2017-01-04
    2017-01-05
    2017-01-06
    2017-01-07
    2017-01-08
    2017-01-09
    2017-01-10
    2017-01-11
    2017-01-12
    2017-01-13
    2017-01-14
    2017-01-15
    2017-01-16
    2017-01-17
    2017-01-18
    2017-01-19
    2017-01-20
    2017-01-21
    2017-01-22
    2017-01-23
    2017-01-24
    2017-01-25
    2017-01-26
    2017-01-27
    2017-01-28
    2017-01-29
    2017-01-30
    ...2017\2017-02
    ...2017\2017-02\2017-02-01
    2017-02-02
    2017-02-03
    2017-02-04
    2017-02-05
    2017-02-06
    2017-02-07
    2017-02-08
    2017-02-09
    2017-02-10
    2017-02-11
    2017-02-12
    2017-02-13
    2017-02-14
    2017-02-15
    2017-02-16
    2017-02-17
    2017-02-18
    2017-02-19
    2017-02-20
    2017-02-21
    2017-02-22
    2017-02-23
    2017-02-24
    2017-02-25
    2017-02-26
    2017-02-27
    2017-02-28
    2017-02-29
    2017-02-30

    And so on...

    Here is my current code:
    Option Explicit
       
    Const cPATH As String = "P:\OS0101_Fort_Hills_MPG\010_FH_GDA\011-GDA_INSPECTION\02 - DAILY GDA FIELD INSPECTIONS\2017\"
    Const cEXT As String = ".xlsx"
    
    
    Sub CheckIfFileExists()
    
       Dim lFirstRow        As Long
       Dim lLastRow         As Long
       Dim c                As Excel.Range
    
       On Error GoTo Catch
       
       lFirstRow = 2
       lLastRow = Range("A" & Rows.Count).End(xlUp).Row
       
       frmXXX.Show vbModeless
       AddStatus "Started"
       
       For Each c In Range(Cells(lFirstRow, 1), Cells(lLastRow, 1))
          
          AddStatus "Processing cell " & c.Address & " - Looking for " & StrConv(c.Value, vbUpperCase)
          
          If c.Value <> vbNullString Then
    
             CheckFolders cPATH, c
             
          End If
       Next
       
       AddStatus "Finished"
       
       Exit Sub
       
    Catch:
    
       MsgBox "ERROR: [" & Err.Number & "] " & Err.Description & vbCrLf & "Proc: CheckIfFileExists     Line Number: " & Erl(), vbExclamation, "Error"
       
    End Sub
    
    Function CheckFolders(ByVal sPath As String, ByRef c As Excel.Range) As Boolean
    
       Dim FSO              As Object
       Dim oFolder          As Object
       Dim oSubFolder       As Object
       Dim FileName         As String
    
       On Error GoTo Catch
    
       Set FSO = CreateObject("Scripting.FileSystemObject")
       Set oFolder = FSO.GetFolder(sPath)
    
       For Each oSubFolder In oFolder.SubFolders
          
          AddStatus "     Checking folder " & oSubFolder
          If Dir(oSubFolder.Path & "\" & c.Value & cEXT) <> vbNullString Then
             CheckFolders = True
             AddStatus "** FOUND **"
             
                If InStr(c.Value, "-") > 0 Then
                   c.Value = Left(c.Value, InStr(c.Value, "-") - 1) & "-" & Val(Mid(c.Value, InStr(c.Value, "-") + 1) + 1)
                Else
                   c.Value = c.Value & "-1)"
                End If
                AddStatus "Cell " & c.Address & " updated to " & c.Value
                AddStatus "------------------------------------------"
          Else
             CheckFolders = CheckFolders(oSubFolder.Path, c)
          End If
       Next
       
       Exit Function
    
    Catch:
    
       MsgBox "ERROR: [" & Err.Number & "] " & Err.Description & vbCrLf & "Proc: CheckFolders      Line Number: " & Erl(), vbExclamation, "Error"
    
    End Function
    
    Public Sub AddStatus(strStatus As String)
    
       On Error GoTo Catch
       
       With frmXXX.lb
          .AddItem Format(Now, "hh:mm:ss")
          .List(.ListCount - 1, 1) = strStatus
          
          If frmXXX.chkReview.Value = False Then
             .ListIndex = .ListCount - 1
          End If
          
       End With
       
       DoEvents
       
       Exit Sub
       
    Catch:
    
       MsgBox "ERROR: [" & Err.Number & "] " & Err.Description & vbCrLf & "Proc: AddStatus      Line Number: " & Erl(), vbExclamation, "Error"
       
    End Sub
    With a form

    
    Option Explicit
    
    Private Sub CommandButton1_Click()
       
       Unload Me
       
    End Sub
    
    Private Sub lb_Click()
    
    End Sub
    
    Private Sub UserForm_Initialize()
          
       
       With lb
          .ColumnCount = 2
          .ColumnWidths = "255, 400"
       End With
       
    End Sub

  2. #2
    Forum Contributor
    Join Date
    03-17-2017
    Location
    Alberta, Canada
    MS-Off Ver
    365 MSO Version 2407 Build 16.0.17830.20166
    Posts
    133

    Re: Can you control which order Subfolders are searched?

    Here is a sample (from the start) of the order the code is currently searching in
    ….2017\2017-07

    ….2017\2017-07\2017-07-09

    ….2017\2017-07\2017-07-28

    ….2017\2017-07\2017-07-25

    ….2017\2017-07\2017-07-20

    ….2017\2017-07\2017-07-15

    ….2017\2017-07\2017-07-23

    ….2017\2017-07\2017-07-02

    ….2017\2017-07\2017-07-11

    ….2017\2017-07\2017-07-27

    ….2017\2017-07\2017-07-13

    ….2017\2017-07\2017-07-16

    ….2017\2017-07\2017-07-30

    ….2017\2017-07\2017-07-01

    ….2017\2017-07\2017-07-04

    ….2017\2017-07\2017-07-24

    ….2017\2017-07\2017-07-31

    ….2017\2017-07\2017-07-29

    ….2017\2017-07\2017-07-05

    ….2017\2017-07\2017-07-22

    ….2017\2017-07\2017-07-10

    ….2017\2017-07\2017-07-07

    ….2017\2017-07\2017-07-03

    ….2017\2017-07\2017-07-06

    ….2017\2017-07\2017-07-12

    ….2017\2017-07\2017-07-18

    ….2017\2017-07\2017-07-08

    ….2017\2017-07\2017-07-17

    ….2017\2017-07\2017-07-21

    ….2017\2017-07\2017-07-26

    ….2017\2017-07\2017-07-19

    ….2017\2017-07\2017-07-14

    ….2017\2017-07\2017-09

    ….2017\2017-07\2017-09-05

    ….2017\2017-07\2017-09-17

    ….2017\2017-07\2017-09-01

    ….2017\2017-07\2017-09-25

    ….2017\2017-07\2017-09-15

    ….2017\2017-07\2017-09-06

    ….2017\2017-07\2017-09-13

    ….2017\2017-07\2017-09-09

    ….2017\2017-07\2017-09-08

    ….2017\2017-07\2017-09-04

    ….2017\2017-07\2017-09-10

    ….2017\2017-07\2017-09-30

    ….2017\2017-07\2017-09-03

    ….2017\2017-07\2017-09-19

    ….2017\2017-07\2017-09-24

    ….2017\2017-07\2017-09-14

    ….2017\2017-07\2017-09-28

    ….2017\2017-07\2017-09-21

    ….2017\2017-07\2017-09-22

    ….2017\2017-07\2017-09-26

    ….2017\2017-07\2017-09-02

    ….2017\2017-07\2017-09-23

    ….2017\2017-07\2017-09-18

    ….2017\2017-07\2017-09-16

    ….2017\2017-07\2017-09-12

    ….2017\2017-07\2017-09-20

    ….2017\2017-07\2017-09-29

    ….2017\2017-07\2017-09-27

    ….2017\2017-07\2017-09-11

    ….2017\2017-07\2017-09-07

    ….2017\2017-08

    ….2017\2017-07\2017-08-05

    ….2017\2017-07\2017-08-29

    ….2017\2017-07\2017-08-18

    ….2017\2017-07\2017-08-31

    ….2017\2017-07\2017-08-17

    ….2017\2017-07\2017-08-06

    ….2017\2017-07\2017-08-25

    ….2017\2017-07\2017-08-11

    ….2017\2017-07\2017-08-20

    ….2017\2017-07\2017-08-19

    ….2017\2017-07\2017-08-12

    ….2017\2017-07\2017-08-03

    ….2017\2017-07\2017-08-13

    ….2017\2017-07\2017-08-04

    ….2017\2017-07\2017-08-09

    ….2017\2017-07\2017-08-10

    ….2017\2017-07\2017-08-27

    ….2017\2017-07\2017-08-16

    ….2017\2017-07\2017-08-23

    ….2017\2017-07\2017-08-22

    ….2017\2017-07\2017-08-15

    ….2017\2017-07\2017-08-07

    ….2017\2017-07\2017-08-02

    ….2017\2017-07\2017-08-24

    ….2017\2017-07\2017-08-30

    ….2017\2017-07\2017-08-21

    ….2017\2017-07\2017-08-01

    ….2017\2017-07\2017-08-26

    ….2017\2017-07\2017-08-14

    ….2017\2017-07\2017-08-08

    ….2017\2017-07\2017-08-28

    ….2017\2017-07\2017-04

    ….2017\2017-07\2017-04-18

    ….2017\2017-07\2017-04-11

    ….2017\2017-07\2017-04-21

    ….2017\2017-07\2017-04-23

    ….2017\2017-07\2017-04-25

    ….2017\2017-07\2017-04-17

    ….2017\2017-07\2017-04-26

    ….2017\2017-07\2017-04-22

    ….2017\2017-07\2017-04-09

    ….2017\2017-07\2017-04-01

    ….2017\2017-07\2017-04-12

    ….2017\2017-07\2017-04-10

    ….2017\2017-07\2017-04-20

    ….2017\2017-07\2017-04-02

    ….2017\2017-07\2017-04-05

    ….2017\2017-07\2017-04-28

    ….2017\2017-07\2017-04-08

    ….2017\2017-07\2017-04-19

    ….2017\2017-07\2017-04-30

    ….2017\2017-07\2017-04-06

    ….2017\2017-07\2017-04-15

    ….2017\2017-07\2017-04-16

    ….2017\2017-07\2017-04-27

    ….2017\2017-07\2017-04-29

    ….2017\2017-07\2017-04-04

    ….2017\2017-07\2017-04-03

    ….2017\2017-07\2017-04-14

    ….2017\2017-07\2017-04-13

    ….2017\2017-07\2017-04-24

    ….2017\2017-07\2017-04-07

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Can you control which order Subfolders are searched?

    As far as I'm aware there is no way in VBA to specify which 'direction'/'order'/whatever to search folders/subfolders.

    I know it's possible in other languages but not VBA.

    What are the filenames you are searching for anyway?

    Is 04062017003-1 an example of a filename?
    If posting code please use code tags, see here.

  4. #4
    Forum Contributor
    Join Date
    03-17-2017
    Location
    Alberta, Canada
    MS-Off Ver
    365 MSO Version 2407 Build 16.0.17830.20166
    Posts
    133

    Re: Can you control which order Subfolders are searched?

    Yes it is, the file names are always 11 digits

    The file name is based on the date the "package" was issued. then the "-" is the number of times it has been inspected.

    So the filenames are "package issued date" & "inspection number"

    This and the file location was not designed by me, and I personally don't like the arrangement, this is how its been done historically, and I'm trying to work with what I got.

  5. #5
    Forum Contributor
    Join Date
    03-17-2017
    Location
    Alberta, Canada
    MS-Off Ver
    365 MSO Version 2407 Build 16.0.17830.20166
    Posts
    133

    Re: Can you control which order Subfolders are searched?

    I can think of only a couple solutions then, none of which I have the ability to write the code for.

    Option A:
    If file is found, start search from beginning again, do this until not found

    Option B:
    Use a reverse order (we should never have a -8), Search for "filename" & "-8", if not found search for "filename" & "-7",...etc until say, "filename" &"-3" was found, then rename cell "-4"

    Option C
    Some crazy search that searches for the first 11 digits and through some wizardry defines a unique "-" number.

  6. #6
    Forum Contributor
    Join Date
    03-17-2017
    Location
    Alberta, Canada
    MS-Off Ver
    365 MSO Version 2407 Build 16.0.17830.20166
    Posts
    133

    Re: Can you control which order Subfolders are searched?

    Edit to Option B:
    there has never been a -6, so we could start there

    This is actually the method I would prefer, I think it may produce the quickest result.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] list of subfolders in folder - without files and sub-subfolders
    By MartyZ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-11-2022, 10:56 AM
  2. [SOLVED] Any Faster Way to Copy/Paste this range? (I have searched and searched and searched)
    By liquidmettle in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 12-12-2015, 02:33 PM
  3. Using activex control to retrieve data relevant to the term searched.
    By garyaddis1983 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-05-2015, 11:13 AM
  4. Create folders in all the subfolders and move subfolders
    By Amarjeet Singh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-24-2015, 12:51 PM
  5. Userform control order
    By mikes1098 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-11-2014, 08:39 PM
  6. [SOLVED] Files within Multiple SubFolders and SubFolders Within It
    By codeslizer in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-18-2013, 04:18 AM
  7. Control X axis order
    By StephanieH in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 04-18-2006, 04:10 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1