sorting filenames from a filedialog selection

Andyatwork

Board Regular
Joined
Mar 29, 2010
Messages
94
Hello Hivemind,

I’m coding a workbook that allows a user to select 3 files from an open file dialog box and have them automatically pasted and formatted to a prescribed reporting requirement.

It works provided the files are sorted A to Z in the folder before the user selects them, but it falls over if the folder is sorted Z to A. I don’t want to rely on the user sorting by filename before selecting the files.
The pasting and formatting performed is dependent on letter flags in the filenames. e.g.,
Code:
            filepath = vrtSelectedItem
            filetype = Left(Right(filepath, 8), 1)              
            fileflag = Left(Right(filepath, 17), 1)

Specifically, data in one file must be pasted after data in another file. B must follow A, not the other way round.

Reading around leads me to believe that sorting
Code:
Each vrtSelectedItem In .SelectedItems
from a filedialog isn’t possible.

While I can probably bodge something clunky I am going to assume that there is a more elegant solution I don’t know about and was hoping someone could point me in the right direction?

Something to do with arrays maybe? I'm thinking I could write the filepaths from SelectedItems to an array, sort the array alphabetically and squirt the array back into Selecteditems, or just run the rest of my code on the array filepaths in turn. My Array-Fu is weak though.

Hope you can help, many thanks.

Andy
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
What code do you have for selecting the files?
 
Upvote 0
Code:
'   Create a FileDialog object as a File Picker dialog box.
'   Open file browser window, allow user to select one or more files
    Set fd = Application.FileDialog(msoFileDialogFilePicker) 
'   Code inside "With...End With" block will be carried out on all files picked
    With fd
    
'   Use the Show method to display the File Picker dialog box and return the user's action.
        If .Show = -1 Then          '   The user pressed the button
        
            Application.StatusBar = "Processing files>>>>>>>"
'   Step through each item in the FileDialogSelectedItems collection.
            For Each vrtSelectedItem In .SelectedItems
            i = i + 1           '   update counter
'   First loop sets up output workbook
            If i = 1 Then
                Application.SheetsInNewWorkbook = 1
                Set opwb = Application.Workbooks.Add
                Application.SheetsInNewWorkbook = 3
and then it goes into checking for markers in filenames and the formatting case select.
 
Last edited:
Upvote 0
How about something like
Code:
Dim lst As Object
Dim Fl As Variant
Dim Wbk As Workbook

Set lst = CreateObject("system.collections.arraylist")
For Each vrtSelectedItem In .SelectedItems
   lst.Add vrtSelectedItem
Next vrtSelectedItem
lst.Sort
For Each Fl In lst
   Set Wbk = Workbooks.Open(Fl)
Next Fl
 
Upvote 0
Could you confirm if I am understanding this correctly?

The filedialog opens and the user picks the files as normal and your code writes those filepaths to this arraylist object, which can the be sorted, then the rest of the code works on that array list instead of the SelectedItems collection? Is that about the size of it?
 
Upvote 0
Hmm, seems to work but...

When it gets to the red text, which I don't really understand but I think it is the moment when copied information is actually pasted in, I get a runtime 1004 error.

In fact that whole with destcell.parent.querytables is greek to me. I hacked it in from something I found on the web and don't know why it works or how to fix it now it is broken. Any chance you can shed light on it?

If I comment it out, nothing gets pasted.


Many thanks,
Code:
'   check file path for Awards indicator and copy to opwb
            filepath = vrtFile
            filetype = Left(Right(filepath, 8), 1)             
            fileflag = Left(Right(filepath, 17), 1)             
'   paste selected file into appropriate tab, starting on row 10 (below header rows)
'   row 10 is first data row 
            
            Select Case filetype
                Case Is = "s"       'Awards
                     Set destCell = opwb.Sheets("Awards_V3").Range("A10")
                     
                Case Is = "t"       
                    If fileflag = "P" Or fileflag = "R" Then       
                        Set destCell = opwb.Sheets("Out_V3").Range("A10")
                    Else                                            
                        Set destCell = opwb.Sheets("Out_V3").Cells _
                            (Rows.Count, "A").End(xlUp).Offset(1)
                    End If
            End Select
            
            With destCell.Parent.QueryTables.Add(Connection:="TEXT;" & _
                vrtSelectedItem, Destination:=destCell)
                .TextFileColumnDataTypes = Array(1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
                .TextFileStartRow = 10                          '   first detail row
                .TextFileParseType = xlDelimited
                .TextFileCommaDelimiter = True
               [B][COLOR=#FF0000] .Refresh BackgroundQuery:=False[/COLOR][/B]
            End With
 
Upvote 0
As I know nothing abut QueryTables, I'd suggest that you start a new thread.
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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