Starting at certain file while traversing directory

L

Legacy 96851

Guest
Pretty much the thread title is what I need to do. The files in the directory have to be in alphabetical order, and I need my macro to collect data from all of them. However, it should start at the letter T, not A (then after it gets to the end (Z), loop back around from the beginning (A-S)). Here is the code I currently have that does them in alphabetical order.
Code:
    sourceName = ActiveWorkbook.Name
    sourcePath = ActiveWorkbook.Path & "\"
 
    sumfile = Dir(sourcePath & "*.xls")
    While sumfile <> ""
            'stuff it does with the file'
        sumfile = Dir()
    Wend
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
You could have a couple of loops like this:
Code:
Sub Test()

    SourceName = ActiveWorkbook.Name
    sourcePath = ActiveWorkbook.Path & "\"
 
    Dim firstChar As Long
    
    For firstChar = Asc("T") To Asc("Z")
        sumfile = Dir(sourcePath & Chr(firstChar) & "*.xls")
        While sumfile <> ""
            'stuff it does with the file'
            sumfile = Dir()
        Wend
    Next
    
    For firstChar = Asc("A") To Asc("S")
        sumfile = Dir(sourcePath & Chr(firstChar) & "*.xls")
        While sumfile <> ""
            'stuff it does with the file'
            sumfile = Dir()
        Wend
    Next
    
End Sub
 
Upvote 0
You could have a couple of loops like this:
Code:
Sub Test()
 
    SourceName = ActiveWorkbook.Name
    sourcePath = ActiveWorkbook.Path & "\"
 
    Dim firstChar As Long
 
    For firstChar = Asc("T") To Asc("Z")
        sumfile = Dir(sourcePath & Chr(firstChar) & "*.xls")
        While sumfile <> ""
            'stuff it does with the file'
            sumfile = Dir()
        Wend
    Next
 
    For firstChar = Asc("A") To Asc("S")
        sumfile = Dir(sourcePath & Chr(firstChar) & "*.xls")
        While sumfile <> ""
            'stuff it does with the file'
            sumfile = Dir()
        Wend
    Next
 
End Sub

Thanks a lot. Sort of already circumvented the problem using cuts and inserts, but I like this way better, so I'll check it out tomorrow.
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

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