+ Reply to Thread
Results 1 to 3 of 3

Cycle through all .xlsb files in a folder in size order (largest first)

Hybrid View

  1. #1
    Registered User
    Join Date
    10-31-2013
    Location
    Edinburgh, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    10

    Cycle through all .xlsb files in a folder in size order (largest first)

    Is it possible to create excel vba code to cycle through and open all .xlsb files in a particular folder starting with the largest filesize first?
    All methods I have seen so far use alpha order.
    Many thanks for any help you can give.

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

    Re: Cycle through all .xlsb files in a folder in size order (largest first)

    The only way I can think of doing it would be to first read in the names and sizes of all the xlsb files in the folder to a list, sort the list by size and then loop through the list opening the files.

    Something like this perhaps.
    Dim strPath As String
    Dim strFileName As String
    Dim I As Long
    
        strPath = "C:\Test\"
    
        strFileName = Dir(strPath & "*.xlsb")
    
        Range("A1:B1").Value = Array("FileName", "Size (bytes)")
    
        Do While Len(strFileName) <> 0
    
            With Range("A" & Rows.Count).End(xlUp).Offset(1)
                .Value = strPath & strFileName
                .Offset(, 1).Value = FileLen(.Value)
            End With
    
            strFileName = Dir()
        Loop
    
        With Worksheets("Sheet1")
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=.Range("A1").CurrentRegion.Columns(2) _
                                      , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With .Sort
                .SetRange Worksheets("Sheet1").Range("A1").CurrentRegion
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        End With
        
        For I = 2 To Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
            Set wbOpen = Workbooks.Open(Worksheets("Sheet1").Range("A" & I))
            
            ' do stuff with workbook
            
            wbOpen.Close SaveChanges:=True
            
        Next I
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    10-31-2013
    Location
    Edinburgh, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Cycle through all .xlsb files in a folder in size order (largest first)

    Thank you so much for your prompt response.
    The code worked perfectly and I would never have thought to do it that way.

+ 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. VBA to list only new files in folder or in order
    By acowan2307 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-25-2021, 12:22 AM
  2. Counting files in folder including subfolders also and folder size
    By mido609 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2012, 03:26 PM
  3. Macro to choose folder and cycle through all files
    By Mr.Pinches in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-08-2011, 04:04 PM
  4. [SOLVED] Getting number of files within a sub-folder and its size
    By Hari in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2006, 01:35 PM
  5. opening order macro for files in a folder
    By Sanbitter Man in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-24-2006, 06:15 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