VBA - open all workbooks in a specific order

biubiubiu

New Member
Joined
Jul 5, 2016
Messages
43
Hi Guys,

I have a bunch of excel workbooks with names like these :

BOCOM_HK_719286_06260329_P1
BOCOM_HK_719286_06260329_P2
BOCOM_HK_719286_06260329_P3
...
BOCOM_HK_719286_06260329_P11


I need to open these files in the ascending numeric order (p1,p2,p3,p4 etc.)

however, currently the opening order is:
BOCOM_HK_719286_06260329_P1
BOCOM_HK_719286_06260329_P10
BOCOM_HK_719286_06260329_P2
BOCOM_HK_719286_06260329_P20
BOCOM_HK_719286_06260329_P3
BOCOM_HK_719286_06260329_P4
...

The code seems to priortize p10 over p2 and p20 over p3 etc.

What changes should I make to acheived the desired results?

Thanks.:)

The related section of my code is as follows:

Code:
Sub BOCOM()

Dim MyFolder As String
Dim MyFile As String
Dim wb As Workbook

MyFolder = "C:\Users\" & user & "\Desktop\New folder"
MyFile = Dir(MyFolder & "\*.csv")
Do While MyFile <> ""
Set wb = Workbooks.Open(filename:=MyFolder & "\" & MyFile)
wb.Close savechanges:=False

MyFile = Dir
Loop
 
Hi WBD
I cant seem to reproduce the same results from my end, even if I set up the files in the folder with exactly the same name (the debug print still shows it is opening p10 before p2).

I guess I will leave this for now and revisit this problem after I gain more VBA coding knowledge (currently on page63 of <VBA and MACROs Excel 2013> :LOL:).

Thanks for all your help.:)
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi Daverunt

This works perfectly for my case, since each folder has only a maximum of around 30 files.

All I need to do is add a few more line of code to define a new variable, which represents the string "BOCOM_HK_719286_06260329_P" because it will be always be different.


Thanks for the help :LOL:
 
Upvote 0

Forum statistics

Threads
1,216,460
Messages
6,130,771
Members
449,589
Latest member
Hana2911

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