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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,795
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
L

Legacy 96851

Guest
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,191,484
Messages
5,986,856
Members
440,053
Latest member
jhollingworth

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
Top