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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,991
Office Version
365
Platform
Windows
What code do you have for selecting the files?
 

Andyatwork

Board Regular
Joined
Mar 29, 2010
Messages
94
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:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,991
Office Version
365
Platform
Windows
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
 

Andyatwork

Board Regular
Joined
Mar 29, 2010
Messages
94
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?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,991
Office Version
365
Platform
Windows
Yes, that's exactly right :)
 

Andyatwork

Board Regular
Joined
Mar 29, 2010
Messages
94
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,991
Office Version
365
Platform
Windows
As I know nothing abut QueryTables, I'd suggest that you start a new thread.
 

Andyatwork

Board Regular
Joined
Mar 29, 2010
Messages
94
I shall poke at it some more and do some research.

Thank you kindly for the pointers.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,991
Office Version
365
Platform
Windows
You're welcome
 

Forum statistics

Threads
1,081,545
Messages
5,359,445
Members
400,528
Latest member
Ratish52

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top