Sorting Filenames in GetOpenFilename Array

AZRFDude

New Member
Joined
May 13, 2003
Messages
2
Guys and Gals,

I have written(hacked) some VB code to open some files, copy the data from a particular sheet, and paste this data on a new worksheet in a new workbook.

This part works well, however the files that I specify to be opened don't open up in the order that I need them to. Here is an example of the filenames that I open for data analysis:

Data - 20030512-20030512 - 1
Data - 20030511-20030511 - 1
Data - 20030510-20030510 - 1

When I select these files, in the file window, the order in which these files open tends to vary. I need for my macro to open these files in order and process the data from the earliest date to the latest (current date).

What would be the easiest method to this? Here is an example of the code I currently use:

Sub CombineMultipleFiles2One()
'
Dim varFilenames As Variant
Dim strActiveBook As String
Dim strSourceDataFile As String
Dim strTest As String
Dim wSht As Worksheet
Dim allwShts As Sheets
'
intResponse = MsgBox("This script will combine all of the sector total data from all selected files to a single worksheet in a new workbook. Continue?", vbOKCancel, "Combine Worksheets to One Sheet")
If intResponse = vbOK Then
Workbooks.Add
strActiveBook = ActiveWorkbook.Name
' Create array of filenames; the True is for multi-select
On Error GoTo exitsub
varFilenames = Application.GetOpenFilename(, , , , True)

counter = 1

' ubound determines how many items in the array
On Error GoTo quit
' Workbooks.Add
Application.ScreenUpdating = False
While counter <= UBound(varFilenames)

'Opens the selected files
Workbooks.Open varFilenames(counter)
strSourceDataFile = ActiveWorkbook.Name


Sheets("sector totals").Select
Range("A2:U307").Select
Selection.Copy

'Find end of usedrange in destination file
Workbooks(strActiveBook).Activate
Range("A1").Select
ActiveSheet.UsedRange.Select
lRows = Selection.Rows.Count
ActiveCell.Offset(lRows, 0).Select

' Copy & Paste All including Formatting
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False

Range("A1").Select


Workbooks(strSourceDataFile).Activate
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True

' displays file name in a message box
MsgBox varFilenames(counter) & " Has Been Processed", vbOKOnly + vbInformation, "File Processed"


'increment counter
counter = counter + 1

Wend

quit:
If Err <> 0 Then
MsgBox "An Error Occurred Trying to open the File. Please close any open Excel files and try again", vbOKOnly + vbExclamation, "File Open Error"
On Local Error GoTo 0
End If
End If
exitsub:
On Local Error GoTo 0
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
What version of Excel are you using?

Glenn.
 
Upvote 0

Forum statistics

Threads
1,221,711
Messages
6,161,450
Members
451,707
Latest member
PedroMoss2268

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