VBA to Open Files in numerical order

radonwilson

New Member
Joined
Jun 23, 2021
Messages
49
Office Version
  1. 2019
Platform
  1. Windows
My code is opening files in alphabetical order. I want to open them in numerical order.
I have 11 files in my folder with serial number and their names.
Code is opening file in this way : 1→10→11→2→3→4→5→6→7→8→9

VBA Code:
Option Explicit

Sub check()

Dim FPath As String 'file's folder path
Dim FName As String 'file's name
Dim wbT As Workbook
Dim wbS As Workbook

Set wbT = ThisWorkbook



With Application.FileDialog(msoFileDialogFolderPicker)

    .Title = "Select your Folder"
    .ButtonName = "Select Folder"
    If .Show = 0 Then
    
        MsgBox ("Nothing was Selected")
    
    Else
    
        FPath = .SelectedItems(1) & "\"
            
    End If

End With

FName = VBA.FileSystem.Dir(FPath & "*.csv*")

Do Until FName = ""
    
        If wbT.Sheets(1).Range("A1").Value = "" Then
        
            Set wbS = Application.Workbooks.Open(FPath & FName)
            wbS.Sheets(1).Range("a1").CurrentRegion.Select
                  
        
        Else
        
        End If
        
    
    FName = Dir
    
Loop

End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
In searching the web, I came across this post here: Excel VBA: Open multiple files in a specific order - OzGrid Free Excel/VBA Help Forum

That may be a little complex. Here are some other alternatives to consider:

1. Do the names of these file correspond with when the files were created? If so, maybe open them in the order they were last modified.
See: Excel VBA: Open multiple files in a specific order - OzGrid Free Excel/VBA Help Forum

2. The simplest method, if you have any control over how the file named are created, is to use a two digit number in the file name.
So instead of naming them like: 1→2→3→4→5→6→7→8→9→10→11,
name them like this: 01→02→03→04→05→06→07→08→09→10→11
Then the should open in the order you want without having to jump through a bunch of hoops or use some complex VBA code.
 
Upvote 0
Solution
In searching the web, I came across this post here: Excel VBA: Open multiple files in a specific order - OzGrid Free Excel/VBA Help Forum

That may be a little complex. Here are some other alternatives to consider:

1. Do the names of these file correspond with when the files were created? If so, maybe open them in the order they were last modified.
See: Excel VBA: Open multiple files in a specific order - OzGrid Free Excel/VBA Help Forum

2. The simplest method, if you have any control over how the file named are created, is to use a two digit number in the file name.
So instead of naming them like: 1→2→3→4→5→6→7→8→9→10→11,
name them like this: 01→02→03→04→05→06→07→08→09→10→11
Then the should open in the order you want without having to jump through a bunch of hoops or use some complex VBA code.
I had also searched the web and came across the same solution like yours. I renamed the files with 2 digits and now these are opening in the numerical order as I wanted.
Thank you for the solution, I appreciate it.
 
Upvote 0
You are welcome!
I am glad you got it working the way you want now.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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